Currently I upload files to my bucket with this code:
function upload_file_to_gcs($source, $des) {
global $gcs;
$bucketName = 'example';
$file = fopen($source, 'r');
$bucket = $gcs->bucket($bucketName);
try {
$result = $bucket->upload($file, [
'name' => $des,
'Content-Type' => 'application/zip'
]);
}
}
But now I want to set the Storage Class, this file should be uploaded to. How can I do that?
In the Amazon AWS I was able to simply set the storage class like:
$result = $s3->putObject([
'Bucket' => 'example',
'Key' => 'folder/'.$des,
'StorageClass' => 'STANDARD_IA', // <--- Simple options command
'ServerSideEncryption' => 'AES256',
'SourceFile' => $source,
'Content-Type' => 'application/zip'
]);
But how can I do this with the Google Storage? I couldn't find any option within the documentation:
http://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.31.1/storage/bucket?method=upload
Does this work?
$result = $bucket->upload($file, [
'name' => $des,
'metadata' => [ 'storageClass' => 'regional' ]
]);
Related
I'm trying to upload an image from an external url (another website) to my s3. this is what I've done so far.
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
// AWS Info
$bucketName = 'my-bucket';
$IAM_KEY = 'XXXXXXXXXXXX';
$IAM_SECRET = 'XXXXXXXXXXXX';
// Connect to AWS
try {
$s3 = new S3Client(
[
'credentials' => [
'key' => $IAM_KEY,
'secret' => $IAM_SECRET
],
'version' => 'latest',
'region' => 'ap-southeast-1'
]
);
} catch (Exception $e) {
die("Error: " . $e->getMessage());
}
$binary = file_get_contents('https://blabla.com/imgs.jpg');
$filename = '/storage/test.jpg';
$response = $s3->putObject(
[
'Bucket' => $bucketName,
'Key' => 'obj-'.ceil(rand(1,100000)),
'SourceFile' => $filename,
'Body' => $binary,
'ContentType' => 'image/jpg',
'StorageClass' => 'REDUCED_REDUNDANCY'
]
);
dd($response);
when i run the code, i get this error
Unable to open "test.jpg" using mode "r": fopen(test.jpg): Failed to open stream: No such file or directory
how do i upload to s3? Thank you.
I am uploading files from php to aws s3. I have successfully uploaded the file.
The url it is returning is => https://BUCKETNAME.s3.ap-south-1.amazonaws.com/images1740/1550830121572.jpg
The actual url is => https://s3.ap-south-1.amazonaws.com/BUCKETNAME/images1740/1550830121572.jpg
(bucket name is coming in starting instead at the end of url)
Because of this it is giving me error while loading images => "Specified Key not found"
$source = $source;
$bucket = 'xxxxxxxxxxxxxxxxx';
$keyname = 'images'.$usr_id."/".$name;
// for push
$s3 = S3Client::factory(
array(
'credentials' => array(
'key' => "xxxxxxxxxxxxxx",
'secret' => "xxxxxxxxxxxxxxx"
),
'version' => 'latest',
'region' => 'ap-south-1'
)
);
try {
// Upload data.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $source,
'ServerSideEncryption' => 'AES256',
));
// Print the URL to the object.
print_r($result);
return $result['ObjectURL'] . PHP_EOL;
// print_r($result);
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
Set use_path_style_endpoint to true when initializing the S3 client to have it use the S3 path style endpoint by default when building the object URL. 1
Implementation details has the object URL to be in the path style if the bucket name makes a valid domain name otherwise it fallback to the S3 path style.
You want to keep the later behavior all the time.
$s3 = S3Client::factory(
array(
'credentials' => array(
'key' => "xxxxxxxxxxxxxx",
'secret' => "xxxxxxxxxxxxxxx"
),
'use_path_style_endpoint' => true,
'version' => 'latest',
'region' => 'ap-south-1'
)
);
You can also do as below if you wanted to disable it one-time for the PutObject operation.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $source,
'ServerSideEncryption' => 'AES256',
'#use_path_style_endpoint' => true
));
I am trying to upload images in AWS s3 bucket but all times its uploading 0 bytes in the bucket.
I really don't know why it's happening like this.
Anyone is here experienced the same issue
Here is the snapshot of image and code
https://www.awesomescreenshot.com/image/3369121/5ac921c28f569f45d1b51ac2ce7b6da1
use Aws\S3\S3Client;
public function aws_upload(){
require 'vendor/autoload.php';
if(isset($_FILES["left_front"]) && $_FILES["left_front"]["error"] == 0){
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$filename = $_FILES["left_front"]["name"];
$filetype = $_FILES["left_front"]["type"];
$filesize = $_FILES["left_front"]["size"];
}
// Instantiate an Amazon S3 client.
$s3 = new S3Client([
'version' => 'latest',
'region' => AWS_REGION,
'credentials' => [
'key' => ACCESS_KEY_ID,
'secret' => SECRET_ACCESS_KEY
]
]);
$bucketName = 'demokrishna';
$key = basename($filename);
try {
$result = $s3->putObject([
'Bucket' => $bucketName,
'Key' => $key,
'acl' => 'public-read'
// 'ACL' => 'public-read',
]);
echo $result->get('ObjectURL');
} catch (Aws\S3\Exception\S3Exception $e) {
echo "There was an error uploading the file.\n";
echo $e->getMessage();
}
}
You have supplied bucket, key, and ACL but you have not supplied the file contents. You can do this using either a Body or a SourceFile.
Example with Body:
$result = $s3Client->putObject([
'Bucket' => $bucket,
'Key' => $key,
'Body' => 'Hello world!',
'ACL' => 'public-read'
]);
Example with SourceFile:
$file_Path = 'folder/filename.jpg';
$result = $s3Client->putObject([
'Bucket' => $bucket,
'Key' => $key,
'SourceFile' => $file_Path,
'ACL' => 'public-read'
]);
Also, note that it's 'ACL', not 'acl'. Please read the putObject documentation.
$key_bucket = basename($filename);
$result = $s3->putObject([
'Bucket' => $bucketName, // bucketname
'Key' => $key_bucket,
'StorageClass' => 'REDUCED_REDUNDANCY',
'SourceFile' => $value['tmp_name'] //temporary path
]);
It works for me
I am using dependency injector to setup S3's credentials:
// AWS S3 for PDF
$container['s3_pdf'] = function ($c) {
// Only load credentials from environment variables.
$provider = CredentialProvider::env();
$s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'ap-southeast-2',
'credentials' => $provider
]);
return $s3;
};
Then whenever I want to upload something I'd do:
$result = $this->s3_pdf->putObject(array(
'Bucket' => 'reports.omitted.com',
'Key' => 'temptest1.pdf',
'SourceFile' => 'assets/temp.pdf',
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
I want to be able to upload to S3 from different functions in the code without having to write the bucket name everytime, am I able to have the s3_pdf container return a function that only takes sourcefile and runs some code to figure out the sourcefile & destination & uploads to S3?
I know that I can use a class that would contain this function I'm after and use an object of that class in the functions where I need S3 but I'd rather use the dependency container if there is a way to do so.
This is the simplest example of my suggested wrapper function possible:
class WhateverYourClassIs
{
function putObject( $key, $sourceFile )
{
return $this->s3_pdf->putObject(array(
'Bucket' => 'reports.omitted.com',
'Key' => $ke,
'SourceFile' => $sourceFile,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
}
}
Or with an array
class WhateverYourClassIs
{
function putObject( $overloadedConfig )
{
$baseConfig = array(
'Bucket' => 'reports.omitted.com',
'Key' => NULL,
'SourceFile' => NULL,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array()
);
return $this->s3_pdf->putObject( array_merge_recursive( $baseConfig, $overloadedConfig ) );
}
}
$this->putObject(array(
'Key' => 'temptest1.pdf',
'SourceFile' => assets/temp.pdf'
));
I'm trying to upload a file from my Wordpress application to a S3 bucket by Ajax:
Somehow, I don't get an answer and the script fails with a 500 error when applying the 'putObject' method.
app/ajax.php
require_once 's3/start.php'
//wp_die(var_dump($s3)); Seems to be fine
$upload = $s3->putObject([
'Bucket' => $config['s3']['bucket'],
'Key' => 'video,
'Body' => fopen( $_FILES['file']['tmp_name'], 'r' ),
'ACL' => 'public-read',
]);
if ($upload) {
wp_die('Uploaded');
} else {
wp_die('Upload Error');
}
app/s3/start.php
use Aws\S3\S3Client;
require 'aws/aws-autoloader.php';
$config = require('config.php');
$s3 = new S3Client([
'key' => $config['s3']['key'],
'secret' => $config['s3']['secret'],
'region' => $config['s3']['region'],
'version' => 'latest',
]);
app/s3/aws
Latest version of the official AWS SDK for PHP
SOLUTION
The credentials in app/start.php where not assigned correctly when initialising the $s3 object. That's how it must look like
$s3 = S3Client::factory([
'region' => $config['s3']['region'],
'version' => 'latest',
'credentials' => [
'key' => $config['s3']['key'],
'secret' => $config['s3']['secret']
]
]);
If you upload a file, you should use SourceFile instead of Body.
Example code:
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
More info from here - http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpPHP.html