Skip to content

Commit d68c40f

Browse files
author
Taylor Grafft
committed
Adding file to create presigned URL from s3 bucket object.
1 parent fa9c07d commit d68c40f

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

api/presignedURL.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const AWS = require('aws-sdk');
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
// Read the AWS secret access key from the aws.key file
6+
const secretAccessKey = fs.readFileSync(path.join(__dirname, 'config', 'aws.key'), 'utf8').trim();
7+
8+
// Configure AWS with credentials and region
9+
const s3 = new AWS.S3({
10+
accessKeyId: 'FQAQ3RWRSCR6POF2NCQC',
11+
secretAccessKey: secretAccessKey,
12+
endpoint: 'https://s3.msi.umn.edu',
13+
s3ForcePathStyle: true,
14+
region: 'us-east-1'
15+
});
16+
17+
// Function to list objects in a bucket
18+
const listObjects = async (bucketName) => {
19+
const params = {
20+
Bucket: bucketName
21+
};
22+
23+
try {
24+
const data = await s3.listObjectsV2(params).promise();
25+
console.log('Objects in bucket:', data.Contents);
26+
27+
// Filter the objects to only show zarr files
28+
// const zarrFiles = data.Contents.filter(obj => obj.Key.endsWith('.zarr'));
29+
// console.log('Zarr files in bucket:', zarrFiles);
30+
} catch (err) {
31+
console.error('Error listing objects:', err);
32+
}
33+
};
34+
35+
// Function to generate a presigned URL for an S3 object
36+
const generatePresignedURL = (bucketName, key, expiresIn) => {
37+
const params = {
38+
Bucket: bucketName,
39+
Key: key,
40+
Expires: expiresIn
41+
};
42+
43+
return s3.getSignedUrl('getObject', params);
44+
};
45+
46+
const bucketName = 'midb-cmc-nonhuman';
47+
const key = 'PS-OCT/KQRH/3D Tiles/A1A2/slice_155_tile_33_CH1.mat';
48+
const expiresIn = 60 * 10; // URL expires in 10 minutes
49+
50+
const url = generatePresignedURL(bucketName, key, expiresIn);
51+
console.log('Presigned URL:', url);
52+
53+
// listObjects(bucketName);

0 commit comments

Comments
 (0)