In Python boto3 library, there is a method called generate_presigned_post that let create a pre-signed URL for uploading file using POST. I do not see an equivalent in Java as the things available ("GeneratePresignedUrlRequest") only support: GET, PUT, DELETE, HEAD.
Wondering if I'm missing something, or if people can think of a way to maybe just do it by writting my own generatePresignedPost in Java?
I'm building a service in Java where I want to return a Pre-signed URl allowing to upload multiple files to a specific bucket/key prefix in S3.
I did some prototyping in Python first to understand more and got to this code:
bucket_name = "my-bucket"
key = "test/${filename}"
expires_in = 24*60*60 # 1 day
s3_client = # Initialize Boto S3 Client with Credentials
response = s3_client.generate_presigned_post(bucket_name, key, ExpiresIn=expires_in)
print(response['url'])
print(response['fields'])
(I know that "${filename}" get converted to a "start-width" condition)
The Java version of GeneratePresignedUrlRequest only allows to create PUT pre-signed URL, doesn't let me set "condition" like the POST version. This is why I'm looking to get a generate_presigned_post equivalent in Java.
The Python code end up creating a request that look like:
fields = {'key': 'test/${filename}'}
conditions = [{'bucket': 'my-bucket'}, ['starts-with', '$key', 'test/']]
request_dict = {'url_path': '/my-bucket', 'query_string': {}, 'method': 'PUT', 'headers': {}, 'body': b'', 'url': 'https://s3.us-west-2.amazonaws.com/my-bucket', 'context': {'is_presign_request': True, 'use_global_endpoint': True}}
And what's returned look like:
response['url'] = 'https://my-bucket.s3.amazonaws.com/'
response['fields'] = {'key': 'test/${filename}', 'AWSAccessKeyId': 'ACCESS_KEY', 'x-amz-security-token': 'SECURITY_TOKEN', 'policy': 'VERY_LONG_POLICY', 'signature': 'SIGNATURE'}