My requirement is that, I want to upload a file into AWS S3 bucket through Spring REST api. This api should first generate a Cloudfront url and upload the file using the url generated and finally in the response I should get the complete filepath : cloudfront-url/filename.
Please let me know the steps and configuration required to achieve this.
Thank you.
If the server uploads the file from client to s3, it will create unnecessary overhead on the server. Better approach is the return pre-signed URL to client (browser) and it directly uploads to S3.
See details on how to do it here.
If that bucket is configured for cloudfront then the cloudfront url can be used directly.
See details here for setting up cloudfront for s3.
Related
I'm trying to practice using AWS more and I'm at a point where I can generate a S3 bucket URL. Now that I have that set up I'm trying to put a document (file) into that URL. Is there any useful documentation or things I should know when I try to do that? I can't seem to find anything on the web for Java users. (maybe I'm just bad at searching idk). Thanks
There is AWS SDK for Java provided by Amazon
AWS SDK For Java
Example work with S3
AWS example of loading file to s3:
https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/examples-s3-objects.html#upload-object
and you would need a token to access you account resources from a java client more info here : https://aws.amazon.com/premiumsupport/knowledge-center/create-access-key/
I need to upload static contents like image to AWS S3 and provide a link back so that image can be accessed through Cloudfront CDN (content delivery network). I am new to AWS and I read that s3 bucket is linked to CDN and I believe it's all configuration based. From java code I am able to upload to s3 and get the bucket based url back. How can I retrieve CDN url from java code for the same uploaded image. Could you please help me out here.
The AWS S3 has to be manually linked to AWS CloudFront (Image attached).
Once a Distribution is created, you can see a new domain mapped to the distribution. Using that domain name you should be able to access the CDN URL in your Java code.
We are trying to use aws S3 for storing files. We created a simple REST API in JAVA to upload and retrieve a file.
Clients requesting to update files use our REST API's which provide a presigned url to either PUT/GET the file. We are using AWS SDK for java to generate the pre signed urls.
We need to add some custom metadata to the files when they are being updated on S3. As we dont control the upload to S3 itself, is there a way we can add this information while we are generating the pre signed url? It wont be good to have the clients to provide this information as a part of their request headers.
We stumbled upon the same issue today and were trying to use
// does not work
request.putCustomRequestHeader(Headers.S3_USER_METADATA_PREFIX + "foo", "bar");
which unfortunately does not really work, it adds the metadata but the caller of the presigned url has to still provide the metadata using request headers which is something the client should not have to do.
Finally we found that using GeneratePresignedUrlRequest#addRequestParameter does the job marvellously:
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest("bucket", "yourFile.ending");
request.addRequestParameter(Headers.S3_USER_METADATA_PREFIX + "foo", "bar");
// import com.amazonaws.services.s3.Headers; needed
The presigned url then looks something like
https://bucket.s3.region.amazonaws.com/yourFile.ending?x-amz-meta-foo=bar&X-Amz-Security-Token=...
The metadata can be clearly seen in the url, using Postman to PUT to that file using upload-file in the body creates the file with the correct metadata in the bucket and it is not possible for the client to change the meta-data because that would make the signature no longer match the request.
The only not-so-pretty part about this is having to specify the internal aws header prefix for user metadata.
I wanted to know whether it is possible to perform S3 multipart upload using pre-signed Url using AWS java SDKs.
Unfortunately i am not able to get any clear answer for it, not sure whether it is supported or not.
Thanks in advance !!!
Have you had a chance to examine the documentation? You will want to presign a create multipart upload request. A related issue includes this comment https://github.com/aws/aws-sdk-java-v2/issues/203#issuecomment-663631708 which shows code from the relevant integration test.
Edited 7th June,14
My Android app needs to have a feature where clients can upload their files. I want AWS S3 as my storage. Moreover i dont want to use SECRET_KEY and ACCESS_KEY_ID on client side. What is the the best way to do this. Can someone provide the working code too ?
I read that i can request to AWS for a signed URL and then make client directly upload to that URL. How to achieve this ?
Maybe you can call AWS APIs in step 4.
And please check the AWS STS(Security Token Service) document, "Ways to Get Temporary Security Credentials" section.