Created
January 17, 2023 17:51
-
-
Save tusamni/d0fe8c18221d21a6b1645fc95424867b to your computer and use it in GitHub Desktop.
Get all images from a DigitalOcean Space
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create an api connection to digitalocean spaces | |
export const spacesClient = new S3({ | |
endpoint: "https://nyc3.digitaloceanspaces.com", | |
region: "us-east-1", | |
credentials: { | |
accessKeyId: import.meta.env.SPACES_KEY, | |
secretAccessKey: import.meta.env.SPACES_SECRET, | |
}, | |
}); | |
// list all the objects in a do spaces s3 container | |
export const getAllImages = async () => { | |
const settings = { | |
Bucket: s3Prefix, | |
Prefix: "images/shoots/", | |
}; | |
try { | |
const data = await spacesClient.listObjectsV2(settings); | |
let objects = []; | |
data.Contents.map((obj) => { | |
const filename = path.parse(obj.Key).base; // get the full filename | |
let object = obj.Key; | |
objects.push(object); | |
}); | |
// remove leading "images" from the path | |
objects = objects.map(function (e) { | |
return e.replace("images/", ""); | |
}); | |
objects = objects.filter((val) => val.includes(imageFormat)); | |
return objects; | |
} catch (err) { | |
console.log("Error", err); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment