I'm currently in the process of upgrading my project from SDK v2 to V3 and I'm struggling to access the location URL of the file I'm uploading to my S3 bucket.
Currently the below implementation is unable to access location
since it does not exist within the AbortMultipartUploadCommandOutput
response type.
try {
const x = new Upload({
client: new S3Client({ region: "us-east-2" }),
params: {
Bucket: BUCKET_NAME,
Key: filename.toString(),
Body: buffer
}
})
const response = await x.done()
const locationUrl = response.location
return locationUrl
} catch (error) {
console.log(error)
}
Another solution I've been exploring is using PutObjectCommand
.
const mand = new PutObjectCommand({
Bucket: BUCKET_NAME,
Key: `${path}/${fileName}`,
Body: fileContent,
ContentType: "text/csv"
})
What are the possible solutions to gaining access to my file's information once it has been uploaded onto S3?
I'm currently in the process of upgrading my project from SDK v2 to V3 and I'm struggling to access the location URL of the file I'm uploading to my S3 bucket.
Currently the below implementation is unable to access location
since it does not exist within the AbortMultipartUploadCommandOutput
response type.
try {
const x = new Upload({
client: new S3Client({ region: "us-east-2" }),
params: {
Bucket: BUCKET_NAME,
Key: filename.toString(),
Body: buffer
}
})
const response = await x.done()
const locationUrl = response.location
return locationUrl
} catch (error) {
console.log(error)
}
Another solution I've been exploring is using PutObjectCommand
.
const mand = new PutObjectCommand({
Bucket: BUCKET_NAME,
Key: `${path}/${fileName}`,
Body: fileContent,
ContentType: "text/csv"
})
What are the possible solutions to gaining access to my file's information once it has been uploaded onto S3?
Share Improve this question asked May 15, 2023 at 15:36 Jamie HuntJamie Hunt 4462 gold badges5 silver badges13 bronze badges 1- Checkout stackoverflow./questions/64980652/… You might not actually need the url in output as you can construct it yourself. – karan shah Commented May 15, 2023 at 16:13
4 Answers
Reset to default 3you can narrow the result
returned by upload.done()
and access to the properties.
function isComplete(
output:
| CompleteMultipartUploadCommandOutput
| AbortMultipartUploadCommandOutput,
): output is CompleteMultipartUploadCommandOutput {
return (output as CompleteMultipartUploadCommandOutput).ETag !== undefined;
}
const result = await upload.done();
if (isComplete(result)) {
const key = result.Key;
const location = result.Location;
}
you can create by yourself if response 200
location = https://${BUCKET_NAME}.s3.${AWS_REGION}.amazonaws./${Key}
const response = await x.done()
return type is CompleteMultipartUploadCommandOutput| AbortMultipartUploadCommandOutput
, Location details is returned in CompleteMultipartUploadCommandOutput you can do it like this
const data: CompleteMultipartUploadCommandOutput = await parallelUploads3.done();
if (!data.Location) {
throw new Error('Location not found');
}
return data.Location;
Do this instead
client
const config: S3ClientConfig = {
region: "us-east-2",
maxAttempts: 10,
credentials: {
accessKeyId: access_key_id,
secretAccessKey: secret_access_key,
},
};
export const s3 = () => new S3(config) || new S3Client(config);
upload function
try {
const x = new Upload({
client: s3(),
queueSize: 4,
leavePartsOnError: false,
params: {
ACL: "public-read",
ContentType: "image/jpeg",
Key,
Body: file,
Bucket: AWS_S3_BUCKET_NAME,
},
})
const response = await x.done()
const locationUrl = response.location
return locationUrl
} catch (error) {
console.log(error)
}