9 examples of 'aws s3 getsignedurl' in JavaScript

Every line of 'aws s3 getsignedurl' 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
86export async function signGetRequest(params: Params, bucket: string, key: string, expires?: number): Promise {
87 const s3 = getS3(params);
88
89 return new Promise((resolve, reject) => {
90 const params = {
91 Bucket: bucket,
92 Key: key,
93 Expires: expires,
94 };
95
96 s3.getSignedUrl("getObject", params, (err: any, url: string) => {
97 if (err) {
98 reject(err);
99 return;
100 }
101
102 resolve(url);
103 });
104 });
105}
35getUrl(key: string) {
36 return S3Factory.getS3(this.region).getSignedUrl('getObject', {
37 Bucket: s3Config.buckets[this.region],
38 Key: key,
39 Expires: this.signedUrlExpireSeconds
40 });
41}
23private getPublicUrl(key: string) {
24 return this.s3.getSignedUrl('getObject', {
25 Bucket: getConfig().BUCKET_NAME,
26 Key: key,
27 Expires: 24 * 60 * 30,
28 });
29}
114_sign(method, bucket, path, date, contentType, contentMd5, xAmzHeaders) {
115 const qPos = path.indexOf('?');
116 let queryToSign = '';
117
118 let _path = path;
119 if (qPos >= 0) {
120 const queryPart = path.substr(qPos + 1, path.length);
121 _path = path.substr(0, qPos);
122 queryToSign = AWSRestSigner.extractSubResources(queryPart);
123 }
124
125 const canonicalizedAmzHeaders = AWSRestSigner.canonizeAwzHeaders(xAmzHeaders);
126
127 let canonicalizedResource = '';
128 if (bucket!='') {
129 canonicalizedResource += '/'+bucket;
130 }
131 canonicalizedResource += _path + queryToSign;
132
133 let stringToSign = method + "\n";
134 if (contentMd5) {
135 stringToSign += contentMd5;
136 }
137 stringToSign += "\n";
138
139 if (contentType) {
140 stringToSign += contentType;
141 }
142 stringToSign += "\n";
143
144 stringToSign +=
145 date + "\n" +
146 canonicalizedAmzHeaders +
147 canonicalizedResource;
148
149 if (this.debug) {
150 console.log("-----------");
151 console.log(stringToSign.replace(/\n/g, "\\n\n"));
152 console.log("-----------");
153 }
154
155 return 'AWS ' + this.accessKeyId + ':' + crypto.createHmac('sha1', this.secretAccessKey).update(stringToSign).digest('base64');
156}
171function isAWSSigned (opts) {
172 const auth = opts.headers && (opts.headers.Authorization || opts.headers.authorization)
173 return typeof auth === 'string' ? auth.startsWith('AWS4-') : false
174}
92url(operation, done) {
93 const scope = local(this)
94 const params = scope.opts.request
95
96 // The S3 client seems to be very sensitive about its callback, even passing undefined will
97 // trigger an error about it not being a function
98 return done
99 ? scope.client.getSignedUrl(operation, params, done)
100 : scope.client.getSignedUrl(operation, params)
101}
108export function createSignedUrl (
109 src: string,
110 user: LookerEmbedUser,
111 host: string,
112 secret: string,
113 nonce?: string
114) {
115 const jsonTime = JSON.stringify(Math.floor((new Date()).getTime() / 1000))
116 const jsonNonce = JSON.stringify(nonce || createNonce(16))
117 const params = {
118 external_user_id: JSON.stringify(user.external_user_id),
119 first_name: JSON.stringify(user.first_name),
120 last_name: JSON.stringify(user.last_name),
121 permissions: JSON.stringify(user.permissions),
122 models: JSON.stringify(user.models),
123 group_ids: JSON.stringify(user.group_ids),
124 user_attributes: JSON.stringify(user.user_attributes),
125 external_group_id: JSON.stringify(user.external_group_id),
126 access_filters: JSON.stringify(user.access_filters || {}),
127 user_timezone: JSON.stringify(user.user_timezone),
128
129 force_logout_login: JSON.stringify(user.force_logout_login),
130 session_length: JSON.stringify(user.session_length),
131
132 nonce: jsonNonce,
133 time: jsonTime
134 }
135
136 const embedPath = '/login/embed/' + encodeURIComponent(src)
137
138 const signingParams = {
139 host,
140 embed_path: embedPath,
141 nonce: jsonNonce,
142 time: jsonTime,
143 session_length: params.session_length,
144 external_user_id: params.external_user_id,
145 permissions: params.permissions,
146 models: params.models,
147 group_ids: params.group_ids,
148 external_group_id: params.external_group_id,
149 user_attributes: params.user_attributes,
150 access_filters: params.access_filters
151 }
152
153 const signature = signEmbedUrl(signingParams, secret)
154
155 Object.assign(params, { signature })
156
157 return `https://${host}${embedPath}?${stringify(params)}`
158}
61sign(opts) {
62 const method = opts.method,
63 host = opts.host || '',
64 path = opts.path || opts.pathname,
65 xAmzHeaders = {};
66 let
67 date=null, contentType=null, contentMd5=null,
68 bucket = "";
69
70 const _match = host.match(/^(.*)\.s3\.amazonaws\.com/);
71 if (_match) {
72 bucket = _match[1];
73 } else {
74 bucket = host;
75 }
76
77 if (!opts.headers) {
78 opts.headers = {};
79 }
80
81 for (const key of Object.keys(opts.headers)) {
82 const lcKey = key.toLowerCase();
83
84 switch(lcKey) {
85 case "date":
86 date = opts.headers[key];
87 break;
88
89 case "content-type":
90 contentType = opts.headers[key];
91 break;
92
93 case "content-md5":
94 contentMd5 = opts.headers[key];
95 break;
96
97 default:
98 if(lcKey.slice(0, 6) == "x-amz-") {
99 xAmzHeaders[lcKey] = opts.headers[key];
100 }
101 break;
102 }
103 }
104
105 if (!date) {
106 date = new Date().toUTCString();
107 opts.headers.date = date;
108 }
109
110 opts.headers["Authorization"] = this._sign(method, bucket, path, date, contentType, contentMd5, xAmzHeaders);
111}
51async function presignedPutObject(contentType, bucket, signedUrlExpiryTime, acl, clientId, userId) {
52 if (!config.s3.enabled) {
53 throw new ServerResponseError(403, t("error.s3notEnabled"), null);
54 }
55
56 try {
57 // Create file name to store object in bucket
58 const extension = mime.extension(contentType);
59 const filePath = config.s3.fileDestination ? config.s3.fileDestination : "";
60 const key = `${filePath}${Date.now().toString()}_${clientId}_${userId}.${extension}`;
61
62 const url = await s3.getSignedUrl("putObject", {
63 Bucket: bucket,
64 Key: key,
65 Expires: signedUrlExpiryTime,
66 ACL: acl,
67 ContentType: contentType
68 });
69
70 // Throw exception if url is not valid
71 if (!validateURL(url)) {
72 throw new ServerResponseError(403, t("validation.signedURLInvalidProperties"), null);
73 }
74
75 return {
76 key: key,
77 signedURL: url
78 };
79 } catch (error) {
80 throw error;
81 }
82}

Related snippets