10 examples of 'aws s3 headobject' in JavaScript

Every line of 'aws s3 headobject' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
349function headObjects(contents, bucket, callback) {
350 var keys = [];
351
352 // Get each object in parallel
353 async.each(contents, function(object, cb) {
354 s3.headObject({
355 Bucket: bucket,
356 Key: object.Key
357 }, function(err, data) {
358 if (err) {
359 cb(err);
360 } else {
361 // Add the key attribute
362 data.Key = object.Key;
363 keys.push(data);
364 cb(null);
365 }
366 });
367 },
368 function(err) {
369 if (err) {
370 callback(err);
371 } else {
372 callback(null, keys);
373 }
374 });
375}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

11async function head_object(req, res) {
12 const encryption = s3_utils.parse_encryption(req);
13 const object_md = await req.object_sdk.read_object_md({
14 bucket: req.params.bucket,
15 key: req.params.key,
16 version_id: req.query.versionId,
17 md_conditions: http_utils.get_md_conditions(req),
18 encryption
19 });
20 s3_utils.set_response_object_md(res, object_md);
21 s3_utils.set_encryption_response_headers(req, res, object_md.encryption);
22}
43export function getObject (s3, params) {
44 return s3.getObject(params).promise();
45};
275function retrieveObject(bucketName, keyName, callback) {
276 var s3 = new AWS.S3();
277 var objectBody = "";
278
279 var objectParams = {
280 Bucket: bucketName,
281 Key: keyName
282 };
283
284 async.waterfall([
285 function loadObject(next) {
286 console.log("Checking for object ", keyName, " in bucket ", bucketName);
287 s3.getObject(objectParams, next);
288 }, function handleObject(data, next) {
289 console.log("Got object");
290 objectBody = data.Body.toString('utf8').trim();
291 next(null);
292 }
293 ], function (err, result) {
294 if (err) {
295 console.log("Error looking for object");
296 console.log(err, err.stack);
297 callback(new Error("cannot load object"), null);
298 } else if (objectBody === '') {
299 callback(new Error("cannot load object"), null);
300 } else {
301 callback(null, objectBody);
302 }
303 });
304}
102function headObj(err, headData) {
103 if (err && err.code !== 'NotFound') {
104 console.error('Error on headObject request', err)
105 callback()
106 }
107 else if (err && err.code === 'AccessDenied') {
108 callback(Error('access_denied'))
109 }
110 else {
111 let url = `https://${Bucket}.s3.${process.env.AWS_REGION}.amazonaws.com/${Key}`
112 if (!headData || !headData.LastModified || stats.mtime > headData.LastModified) {
113 let params = {
114 ACL: 'public-read',
115 Bucket,
116 Key,
117 Body: fs.readFileSync(file),
118 }
119 if (getContentType(file)) {
120 params.ContentType = getContentType(file)
121 }
122 if (fingerprint && Key !== 'static.json') {
123 params.CacheControl = 'max-age=315360000'
124 }
125 if (fingerprint && Key === 'static.json') {
126 params.CacheControl = 'no-cache, no-store, must-revalidate, max-age=0, s-maxage=0'
127 }
128 s3.putObject(params, function _putObj(err) {
129 if (err && err.code === 'AccessDenied') {
130 callback(Error('access_denied'))
131 }
132 else if (err) {
133 console.log(err)
134 callback()
135 }
136 else {
137 console.log(`${chalk.blue('[ Uploaded ]')} ${chalk.underline.cyan(url)}`)
138 if (big) console.log(`${chalk.yellow('[ Warning! ]')} ${chalk.white.bold(`${Key} is > 5.75MB`)}${chalk.white(`; files over 6MB cannot be proxied by Lambda (arc.proxy)`)}`)
139 callback()
140 }
141 })
142 }
143 else {
144 console.log(`${chalk.gray('[Not modified]')} ${chalk.underline.cyan(url)}`)
145 if (big) console.log(`${chalk.yellow('[ Warning! ]')} ${chalk.white.bold(`${Key} is > 5.75MB`)}${chalk.white(`; files over 6MB cannot be proxied by Lambda (arc.proxy)`)}`)
146 callback()
147 }
148 }
149})
11public async getObject(key: string, bucket: string) {
12 const params: S3.Types.GetObjectRequest = {
13 Bucket: bucket,
14 Key: key,
15 };
16 return await this.s3
17 .getObject(params)
18 .promise()
19 .then((data: S3.Types.GetObjectOutput) => data)
20 .catch<null>(err => {
21 Utils.logError('S3 Get Object', err, params);
22 return null;
23 });
24}
68async getStatus(file: Object): Object {
69
70 const params = this._paramsProvider.getHeadObjectParams(file);
71 let result;
72 try {
73 result = await this._s3.headObjectAsync(params);
74 } catch (error) {
75 if (error.code !== "NotFound") {
76 throw new S3Error(error);
77 }
78
79 return {
80 status : RemoteStatus.Missing,
81 statusDetails : getStatusDetailsMissing()
82 };
83 }
84
85 const details = getStatusDetails(file, result);
86
87 return {
88 status : details.content === RemoteStatus.Same
89 && details.headers === RemoteStatus.Same
90 && details.remote === RemoteStatus.Same
91 ? RemoteStatus.Same
92 : RemoteStatus.Different,
93 statusDetails : details
94 };
95}
25function getObject(minioClient, bucketName, objectName) {
26 return new Promise((resolve, reject) =>
27 minioClient.getObject(bucketName, objectName, (err, dataStream) => {
28 if (err) {
29 reject(err);
30 return;
31 }
32
33 const chunks = [];
34 dataStream.on("data", chunk => chunks.push(chunk));
35 dataStream.on("error", reject);
36 dataStream.on("end", () => {
37 resolve(Buffer.concat(chunks).toString("utf8"));
38 });
39 })
40 );
41}
97async exists(key: string): Promise<boolean> {
98 return new Promise((resolve, reject) => {
99 this.s3.getObject(
100 {
101 Bucket: this.bucket,
102 Key: this.prefix + key,
103 },
104 (err, data) => {
105 if (err) {
106 resolve(false);
107 } else {
108 resolve(true);
109 }
110 }
111 );
112 });
113}
184export async function testBucketExists(s3Client: S3, bucketName: string): Promise<boolean> {
185 try {
186 await s3Client.headBucket({ Bucket: bucketName }).promise()
187
188 return true
189 } catch (err) {
190 return false
191 }
192}

Related snippets