Skip to content

Instantly share code, notes, and snippets.

@0ex-d
Last active June 24, 2023 23:59
Show Gist options
  • Save 0ex-d/3f8c11bd41b5abc02a16325f853fb3c3 to your computer and use it in GitHub Desktop.
Save 0ex-d/3f8c11bd41b5abc02a16325f853fb3c3 to your computer and use it in GitHub Desktop.
This is used for Cloudflare R2 which has a very similar Cloud infra stetup with AWS S3
const { S3Client, CreateBucketCommand } = require("@aws-sdk/client-s3");
// Replace the following placeholders with your own values
const bucketName = "your-bucket-name";
const region = "your-preferred-region";
// Set up the S3 client
const s3Client = new S3Client({ region });
// Create the S3 bucket
async function createBucket() {
const command = new CreateBucketCommand({ Bucket: bucketName });
try {
const response = await s3Client.send(command);
console.log("Bucket created successfully:", response.Location);
} catch (error) {
console.error("Error creating bucket:", error);
}
}
createBucket();
#!/bin/bash
# Replace the following placeholders with your own values
BUCKET_NAME="your-bucket-name"
REGION="your-preferred-region"
# Create the S3 bucket
aws s3api create-bucket --bucket "$BUCKET_NAME" --region "$REGION"
chmod +x create_bucket.sh
./create_bucket.sh
aws s3api delete-object --bucket your-bucket-name --key path/to/file-in-bucket
aws s3api delete-object --bucket your-bucket-name --key path/to/file1-in-bucket --key path/to/file2-in-bucket --key path/to/file3-in-bucket
const { S3Client, GetObjectCommand } = require("@aws-sdk/client-s3");
const { createWriteStream } = require("fs");
// Replace the following placeholders with your own values
const bucketName = "your-bucket-name";
const key = "path/to/file-in-bucket";
const region = "your-preferred-region";
const localFilePath = "path/to/local/file";
// Set up the S3 client
const s3Client = new S3Client({ region });
// Download the file from S3
async function downloadFile() {
const command = new GetObjectCommand({ Bucket: bucketName, Key: key });
try {
const response = await s3Client.send(command);
const fileStream = createWriteStream(localFilePath);
response.Body.pipe(fileStream);
console.log("File downloaded successfully.");
} catch (error) {
console.error("Error downloading file:", error);
}
}
downloadFile();
aws s3api list-objects-v2 --endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com --bucket <BUCKET> --output text --profile <PROFILE_NAME>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment