File Upload on Amazon S3 server using CURL request :
Use Case : Sometimes we need to upload file on Amazon S3 or need to write code to upload file. As file upload on S3 using API call requires parameters in specific format and debugging that is very cumbersome task, in that case we can use CURL request with the inputs for debugging.
Following are the required Inputs for CURL:
- Date in a specific format RFC 2822.
- Bucket name which is already created on S3.
- File which needs to be uploaded.
- ContentType is the type of file.
- Access Key of S3 server.
- Secret key of S3 Server.
- Base64 Encoded Signature by using S3 secret key.
First we will create signature string using content-type, date and resource.
This string will be base 64 encoded using S3 secret key which will be our signature.
Following is the CURL request :
[code language=”java”]
date=`date +%Y%m%d`
dateFormatted=`date -R`
s3Bucket="BUCKET_OF_S3_SERVER"
fileName="FILE_NAME"
relativePath="/${s3Bucket}/${fileName}"
contentType="application/octet-stream"
stringToSign="PUT\n\n${contentType}\n${dateFormatted}\n${relativePath}"
s3AccessKey="ACCESS_KEY_OF_S3_SERVER"
s3SecretKey="SECRET_KEY_OF_S3_SERVER"
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3SecretKey} -binary | base64`
curl -X PUT -T "${fileName}" \
-H "Host: ${s3Bucket}.s3.amazonaws.com" \
-H "Date: ${dateFormatted}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: AWS ${s3AccessKey}:${signature}" \
http://${s3Bucket}.s3.amazonaws.com/${fileName}
[/code]
Got this error, when I tried this
The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256.
Amazing Post….
Thanks for the nice information.
I have found another relevant blog which is also good and I hope it is useful for others.