Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What parameters does S3Client::putObject() expect? #7

Closed
martinbean opened this issue Nov 13, 2012 · 10 comments
Closed

What parameters does S3Client::putObject() expect? #7

martinbean opened this issue Nov 13, 2012 · 10 comments
Labels
guidance Question that needs advice or information.

Comments

@martinbean
Copy link

I’m having trouble with the putObject() method of the S3Client class. One of the arguments to be passed is Body. The API documentation says this is to be an array, but doesn’t say or give an example of parameters/keys that should be contained in this array.

I have an upload working, but the file being placed on my Amazon S3 set-up is empty because I’m obviously passing some incorrect data to it, and don‘t know what I should be passing. Below is my current code:

$file = 'public/test.jpg';

try {
    $result = $container['amazon_s3']->putObject(array(
        'Bucket' => (string) $container['misc_config']->images->amazon->bucket,
        'Key' => 'images/test_martin/test.jpg',
        'ContentType' => mime_content_type($file),
        'Body' => array(
            'file' => $file,
            'size' => filesize($file)
        )
    ));
    print '<pre>';
    print_r($result);
    exit;
}
catch (\Exception $e) {
    echo $e->getMessage();
    exit;
}

Any help would be much appreciated.

@jeremeamia
Copy link
Contributor

I responded to your forum post earlier today with a fix: https://forums.aws.amazon.com/thread.jspa?threadID=109616&tstart=0

The docs are wrong for the Body option. We will work on getting that fixed.

@martinbean
Copy link
Author

Thanks, @jeremeamia. I tried the wrapping the file name in fopen() as per your suggestion, but get the following exception now:

Validation errors: [Body] must be of type string or object

@martinbean
Copy link
Author

Actually, I think I may have solved it: I used file_get_contents() instead.

@jeremeamia
Copy link
Contributor

Yes, file_get_contents() should work fine.

Also, you can use the EntityBody object from Guzzle in conjunction with fopen:

'Body' => EntityBody::factory(fopen($file, 'r'))

This might work better for larger files since you won't have to load the entire file into memory at once as you would with file_get_contents().

I apologize for misleading you with my previous comment.

@martinbean
Copy link
Author

Hi Jeremy. No problem. Thanks for clearing this up for me! Much appreciated. I’m new to this Guzzle 😉

@martinbean
Copy link
Author

Another question: how do I mark a uploaded file as public? I’m reading the API documentation again (http://docs.amazonwebservices.com/aws-sdk-php-2/latest/class-Aws.S3.S3Client.html) but it’s not too clear the expected values. I’m guessing I want to set something for the ACL key, but not what to set it as. Sample values would be great if possible?

EDIT: Fixed it myself (again)! To mark a file as public, I imported the Aws\S3\Enum\CannedAcl class and used the CannedAcl::PUBLIC_READ class constant.

@jeremeamia
Copy link
Contributor

Yep, you got it! For any enum-like values we have classes under the Enum namespace for that client. Hopefully we can better call information like that out as we improve our docs.

@martinbean
Copy link
Author

Cool. Is there a channel where I can report small things like this that are hard-to-understand or could be better-documented if I come across anything?

@jeremeamia
Copy link
Contributor

Right here on the GitHub issue tracker is going to be the easiest for us to track. Thanks.

@martinbean
Copy link
Author

No problem.

@ghost ghost mentioned this issue Nov 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

2 participants