In AWS SDK Version 2 I was uploading files to S3 and getting their public url in reposonse.
// using ASW SDK version 2
var S3 = require('aws-sdk/clients/s3')
var s3 = new S3({
accessKeyId: config.aws.accessKeyId,
secretAccessKey: config.aws.secretAccessKey,
region: config.aws.region,
apiVersion: '2010-12-01'
});
var uploadToS3 = async function(uploadParams) {
var response = await s3.upload(uploadParams).promise()
return response.Location
}
It was so easy to get url of file after uploading using response.Location
.
Now I have started using AWS SDK for S3 version 3
for doing same thing but I did not find way to get url after uploading file.
// using AWS SDK version 3
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
var awsCredentials = {
region: config.aws.region,
credentials: { accessKeyId: config.aws.accessKeyId, secretAccessKey: config.aws.secretAccessKey }
}
var s3Client = new S3Client(awsCredentials)
var uploadToS3 = async function (uploadParams) {
const data = await s3Client.send(new PutObjectCommand(uploadParams))
if (data.$metadata.httpStatusCode == 200) {
let url = `https://${uploadParams.Bucket}.s3.ap-south-1.amazonaws.com/${uploadParams.Key}`
return url
}
}
In SDK version 3 I dont know way to get url so I need to construct manually which does not handle encoding of url.
I found some way of encoding manually created urls but those ways are not relaible.
S3 is encoding urls with spaces and symbols to unkown format
Amazon S3 URL Encoding
I guess there should be AWS SDK way to get url the way I was getting in SDK version 2.