I'm trying to upload a file to my bucket. I am able to upload with Body but not SourceFile. Here's my method:
$pathToFile='/explicit/path/to/file.jpg'
// Upload an object by streaming the contents of a file
$result = $s3Client->putObject(array(
'Bucket' => $bucket,
'Key' => 'test.jpg',
'SourceFile' => $pathToFile,
'ACL' => 'public-read',
'ContentType' => 'image/jpeg'
));
but I get this error:
You must specify a non-null value for the Body or SourceFile parameters.
I've tried different types of files and keep getting this error.
The issue had to do with not giving a good path. Looks like file_exists() only checks locally, meaning that it had to be within localhost's index.
Change
'SourceFile' => $pathToFile,
to
'Body' => $pathToFile,
Related
I am creating a gzip string and uploading it as an object to s3. However when I download the same file from s3 and decompress it locally with gunzip I get this error: gunzip: 111.gz: not in gzip format When I look at the mime_content_type returned in the file downloaded from s3 it is set as: application/zlib
Here is the code I am running to generate the gzip file and push it to s3:
for($i=0;$i<=100;$i++) {
$content .= $i . "\n";
}
$result = $this->s3->putObject(array(
'Bucket' => 'my-bucket-name',
'Key' => '111.gz',
'Body' => gzcompress($content),
'ACL' => 'authenticated-read',
'Metadata' => [
'ContentType' => 'text/plain',
'ContentEncoding' => 'gzip'
]
));
The strange thing is that if I view the gzip content locally before I send it to s3 I am able to decompress it and see the original string. So I must be uploading the file incorrectly, any thoughts?
According to http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#putobject the ContentType and ContentEncoding parameters belong on top level, and not under Metadata. So your call should look like:
$result = $this->s3->putObject(array(
'Bucket' => 'my-bucket-name',
'Key' => '111.gz',
'Body' => gzencode($content),
'ACL' => 'authenticated-read',
'ContentType' => 'text/plain',
'ContentEncoding' => 'gzip'
));
Also it's possible that by setting ContentType to text/plain your file might be truncated whenever a null-byte occurs. I would try with'application/gzip' if you still have problems unzipping the file.
I had a very similar issue, and the only way to make it work for our file was with a code like this (slightly changed according to your example):
$this->s3->putObject(array(
'Bucket' => 'my-bucket-name',
'Key' => '111.gz',
'Body' => gzcompress($content, 9, ZLIB_ENCODING_GZIP),
'ACL' => 'public-read',
'ContentType' => 'text/javascript',
'ContentEncoding' => 'gzip'
));
The relevant part being gzcompress($content, 9, ZLIB_ENCODING_GZIP), as AWS S3 wouldn't recognize the file nor serve it in the right format without the last ZLIB_ENCODING_GZIP parameter.
here is my code here:
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => 'image.jpg',
'SourceFile' => $_FILES['image'],
'ContentType' => 'image/jpg',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY'
));
as you can see, I want to pass $_FILES['image'] into the SourceFile because that is what I want to upload to AWS S3. how can I do this because the error I get is this:
Fatal error: Uncaught RuntimeException: Unable to open Array using mode r: fopen() expects parameter 1 to be a valid path, array given in
$_FILES['image'] is an array that contains other information about the file upload. You can see all the keys here: http://php.net/manual/en/features.file-upload.post-method.php
Your file is actually at $_FILES['image']['tmp_name'].
I am using Laravel 5.0 and using "aws/aws-sdk-php-laravel": "~2.0"
Here is my script to upload the image
$s3 = App::make('aws')->get('s3');
$s3->putObject(array(
'Bucket' => 'greenhoppingbucket',
'Key' => 'sups',
'Body' => Input::file('file'),
));
After the execution only the key is uploaded in the s3 bucket
i.e., sups is created bucket but not the image.
What is the mistake i am doing and how can i fix this
try this:
$s3 = App::make('aws')->get('s3');
$s3->putObject(array(
'Bucket' => 'greenhoppingbucket',
'Key' => 'sups',
'Body' => File::get((string)Input::file('file')),
));
dont forget to add use File;
when you do 'Body' => Input::file('file'), you bassiclly putting the temp path into the body instead of the content of the file.
the File::get is simply getting the contents of a file
I'm trying to transfer files from Shopify to S3, and I'm getting this error:
"You must specify a non-null value for the Body or SourceFile parameters." I believe it's because I'm transferring files from a remote location but I don't know how to get around this problem. I have validated that the remote file does exist, and that my code works fine if I'm uploading using a form.
My code:
require dirname(__FILE__).'/../../vendor/autoload.php';
use Aws\S3\S3Client;
$s3Client = null;
$bucket="mybucketname";
$myfile="myfilename.jpg";
$mypath="https://shopifyfilepath/".$myfile;
function SendFileToS3($filename, $filepath, $bucket)
{
global $s3Client;
if (!$s3Client)
{
$s3Client = S3Client::factory(array(
'key' => $_SERVER["AWS_ACCESS_KEY_ID"],
'secret' => $_SERVER["AWS_SECRET_KEY"]
));
}
$result = $s3Client->putObject(array(
'Bucket' => $bucket,
'Key' => $filename,
'SourceFile' => $filepath,
'ACL' => 'public-read'
));
return $result;
}
SendFileToS3($myfile, $mypath, $bucket);
You can't transfer a file directly from a HTTP path to S3. You'll need to download it to a local file (or variable) first, then transfer that.
I tried the following two functions, none of them works, they could upload the file to S3 but if you visit the uploaded file from a browser you could see it's being treated as application/octet-stream, that's not right...
$s3->upload('mybucket', // bucket
$filename, // key
$imagebinarydata, // body
'public-read', // acl
array('contentType' => 'image/jpeg')); // options
And
$s3->putObject(array(
'Bucket' => 'mybucket',
'Key' => $filename,
'ACL' => 'public-read',
'contentType' => 'image/jpeg',
'Body' => $imagebinarydata));
I'm using the latest AWS.PHAR
You need to use uppercase ContentType with putObject as described in the docs:
'ContentType' => 'image/jpeg'