Skip to content

Instantly share code, notes, and snippets.

@tusamni
Created January 17, 2023 17:51
Show Gist options
  • Save tusamni/d0fe8c18221d21a6b1645fc95424867b to your computer and use it in GitHub Desktop.
Save tusamni/d0fe8c18221d21a6b1645fc95424867b to your computer and use it in GitHub Desktop.
Get all images from a DigitalOcean Space
// 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