Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uplode image to amazon s3 using @aws-sdk/client-s3 ang get its location

i am trying upload an in image file to s3 but get this error says :

ERROR: MethodNotAllowed: The specified method is not allowed against this resource.

my code using @aws-sdk/client-s3 package to upload wth this code :

const s3 = new S3({
    region: 'us-east-1',
    credentials: {
        accessKeyId: config.accessKeyId,
        secretAccessKey: config.secretAccessKey,
    }
});

exports.uploadFile = async options => {
    options.internalPath = options.internalPath || (`${config.s3.internalPath + options.moduleName}/`);
    options.ACL = options.ACL || 'public-read';

    logger.info(`Uploading [${options.path}]`);
    const params = {
        Bucket: config.s3.bucket,
        Body: fs.createReadStream(options.path),
        Key: options.internalPath + options.fileName,
        ACL: options.ACL
    };

    try {
        const s3Response = await s3.completeMultipartUpload(params);
        if (s3Response) {
            logger.info(`Done uploading, uploaded to: ${s3Response.Location}`);
            return { url: s3Response.Location };
        }
    } catch (err) {
        logger.error(err, 'unable to upload:');
        throw err;
    }
};

I am not sure what this error mean and once the file is uploaded I need to get his location in s3

thanks for any help

like image 529
spinker Avatar asked Nov 17 '25 19:11

spinker


1 Answers

Doesn't seems like they give "Location" in the response anymore.

{
'$metadata': {
  httpStatusCode: 200,
  requestId: '',
  extendedRequestId: '',
  cfId: ,
  attempts: 1,
  totalRetryDelay: 0
},
ETag: '',
ServerSideEncryption: ''
}

This is the response object.

Use this to get the Location

`https://${BUCKETNAME}.s3.${REGION}.amazonaws.com/${KEY}`

Be sure "ACL" is "public-read"

like image 185
Mohammad Rahat Avatar answered Nov 19 '25 08:11

Mohammad Rahat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!