I need to check an object before creating a signed url. I need to use custom Expires. I am using following code.
var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: 'YOURKEY', secretAccessKey:'YOURSECRET'});
var s3 = new AWS.S3()
params = {Bucket:'BUCKET_NAME', Key: 'TEST.pdf', Expires: 10000}
s3.headObject(params, function(err, data) {
if (err) console.log(err,err.code); // an error occurred
else
{
s3.getSignedUrl('getObject', params, function (err, url) {
if (err) {
console.log('error: ' + err);
} else {
console.log('Signed URL: ' + url);
}
});
} // successful response
});
I am getting following error in headObject. I know headObject doesn't support Expiries.
{ [UnexpectedParameter: Unexpected key 'Expires' found in params]
message: 'Unexpected key \'Expires\' found in params',
code: 'UnexpectedParameter',
time: Wed Apr 06 2016 10:14:53 GMT-0500 (CDT) }
How can i use params with custom expires in both headObject and getSignedUrl ?
Here's the exact code to fix your issue. This should really be self evident if you just read the error message you are getting.
var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: 'YOURKEY', secretAccessKey:'YOURSECRET'});
var s3 = new AWS.S3()
headParams = {Bucket:'BUCKET_NAME', Key: 'TEST.pdf'}
urlParams = {Bucket:'BUCKET_NAME', Key: 'TEST.pdf', Expires: 10000}
s3.headObject(headParams, function(err, data) {
if (err) console.log(err,err.code); // an error occurred
else
{
s3.getSignedUrl('getObject', urlParams, function (err, url) {
if (err) {
console.log('error: ' + err);
} else {
console.log('Signed URL: ' + url);
}
});
} // successful response
});
Using V3 client
import {S3Client, HeadObjectCommand} from "@aws-sdk/client-s3";
const client = new S3Client(config);
const input = {
Bucket: "STRING_VALUE", // required
Key: "STRING_VALUE", // required
};
try {
const command = new HeadObjectCommand(input);
const response = await client.send(command);
} catch (e) {
console.log("Object does not exist")
}
Reference : https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/classes/headobjectcommand.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With