CrushedPixel
CrushedPixel

Reputation: 1172

Amazon AWS Java SDK - use S3 Policy and Signature to perform PutObjectRequest

From a Web API, I receive the following information about an Amazon S3 Bucket I am allowed to upload a File to:

s3_bucket (the Bucket name)
s3_key (the Bucket key)
s3_policy (the Bucket policy)
s3_signature the Bucket signature)

Because I am not the owner of the Bucket, I am provided with the s3_policy and s3_signature values, which, according to the AWS Upload Examples, can be used to authenticate a Put request to a Bucket.

However, in AWS's official Java SDK I'm using, I can't seem to find a way to perform this authentication. My code:

PutObjectRequest putObjectRequest = new PutObjectRequest(s3_bucket, s3_key, fileToUpload);
s3Client.putObject(putObjectRequest);

I do understand that I need to use the s3_signature and s3_policy I'm given at some point, but how do I do so to authenticate my PutObjectRequest?

Thanks in advance,

CrushedPixel

Upvotes: 2

Views: 608

Answers (1)

Michael - sqlbot
Michael - sqlbot

Reputation: 179084

I don't think you're going to use the SDK for this operation. It's possible that the SDK will do what you need at this step, but it seems unlikely, since the SDK would typically take the access key and secret as arguments, and generate the signature, rather than accepting the signature as an argument.

What you describe is an upload policy document, not a bucket policy. That policy, the signature, and your file, would all go into an HTTP POST (not PUT) request of type multipart/form-data -- a form post -- as shown in the documentation page you cited. All you should need is an HTTP user agent.

You'd also need to craft the rest of the form, including all of the other fields in the policy, which you should be able to access by base64-decoding it.

The form also requires the AWSAccessKeyId, which looks something like "AKIAWTFBBQEXAMPLE", which is -- maybe -- what you are calling the "s3_key," although in S3 terminology, the "(object) key" refers to the path and filename.

This seems like an odd set of parameters to receive from a web API, particularly if they are expecting you to generate the form yourself.

Upvotes: 1

Related Questions