Claude Code × AWS S3: न्यूनतम अनुमति के साथ sync, इमेज upload, backup और presigned URL
Claude Code और AWS S3 guide: IAM least privilege, aws s3 sync, static site, image upload, backup, presigned URL.
S3 पहली नज़र में file storage जैसा लगता है, लेकिन real application में permission, public access, sync, cache, delete और cost सब जुड़ जाते हैं। Claude Code script और helper जल्दी बना सकता है, पर vague prompt से IAM policy बहुत wide हो सकती है।
यह guide उन beginners के लिए है जो S3 को production के पास सुरक्षित तरीके से उपयोग करना चाहते हैं। हम static site, image upload, backup और presigned URL देखेंगे। मुख्य नियम है least privilege: हर script को सिर्फ जरूरी action और prefix दें।
Examples इन official docs पर आधारित हैं: AWS CLI S3 reference, aws s3 sync reference, Boto3 presigned URL guide, Claude Code common workflows.
Background के लिए ये internal articles भी देखें: IAM, security, context.
Code से पहले S3 use case अलग करें
use case 1 static site है। dist को site/ में sync करते हैं और CloudFront serve करता है। use case 2 images है। Admin uploads/ या assets/images/ में upload करता है। use case 3 backup है। Dump और PDF date prefix में जाते हैं और —delete नहीं लेते। use case 4 private download है, जहाँ bucket private रहता है और app short presigned URL देता है।
सब कुछ mix करने पर s3:* आसान लगता है। बेहतर है prefix तय करें: site/, assets/images/, uploads/, backups/, assets/private-reports/. इन boundaries से Claude Code का output review करना आसान होता है।
Identity check और IAM least privilege
Bucket और region को पहले variable में रखें ताकि आप और Claude Code एक ही target जाँचें।
export AWS_REGION=ap-northeast-1
export S3_BUCKET=claudecode-lab-assets-prod
aws sts get-caller-identity
aws s3 ls "s3://${S3_BUCKET}/" --region "${AWS_REGION}"
यह policy सिर्फ assets पढ़ती है, uploads में लिखती है और बड़े delete permission से बचती है।
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ReadPublicAssetsOnly",
"Effect": "Allow",
"Action": ["s3:GetObject"],
"Resource": "arn:aws:s3:::claudecode-lab-assets-prod/assets/*"
},
{
"Sid": "WriteUploadsOnly",
"Effect": "Allow",
"Action": ["s3:PutObject", "s3:AbortMultipartUpload"],
"Resource": "arn:aws:s3:::claudecode-lab-assets-prod/uploads/*"
},
{
"Sid": "ListLimitedPrefixes",
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": "arn:aws:s3:::claudecode-lab-assets-prod",
"Condition": {
"StringLike": {
"s3:prefix": ["assets/*", "uploads/*", "backups/*"]
}
}
}
]
}
Static site या image folder sync करने का यह basic रूप है। पहले dryrun जरूर चलाएँ।
# 1. Preview changes first. This should become a habit.
aws s3 sync ./dist "s3://${S3_BUCKET}/site/" \
--region "${AWS_REGION}" \
--delete \
--cache-control "public,max-age=300" \
--dryrun
# 2. Deploy only after the preview looks right.
aws s3 sync ./dist "s3://${S3_BUCKET}/site/" \
--region "${AWS_REGION}" \
--delete \
--cache-control "public,max-age=300"
# 3. Upload long-lived images with a different cache policy.
aws s3 sync ./public/images "s3://${S3_BUCKET}/assets/images/" \
--region "${AWS_REGION}" \
--exclude "*.psd" \
--cache-control "public,max-age=31536000,immutable"
Presigned URL bucket को private रखते हुए थोड़े समय के लिए download access देता है।
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
const s3 = new S3Client({ region: process.env.AWS_REGION ?? "ap-northeast-1" });
export async function createDownloadUrl(key: string, filename: string) {
if (!key.startsWith("assets/private-reports/")) {
throw new Error(`Unexpected S3 key prefix: ${key}`);
}
const command = new GetObjectCommand({
Bucket: process.env.S3_BUCKET_NAME,
Key: key,
ResponseContentDisposition: `attachment; filename="${filename}"`,
});
return getSignedUrl(s3, command, { expiresIn: 900 });
}
Claude Code को task देते समय goal, forbidden actions और verification commands साथ दें।
このリポジトリに AWS S3 連携を追加してください。
目的: public/images を S3 の assets/images/ に同期し、private-reports/ のPDFだけ署名付きURLで配布する。
制約: バケット全体公開は禁止。s3:DeleteObject は付けない。aws s3 sync は必ず --dryrun を先に出す。
成果物: scripts/s3-sync-assets.mjs、lib/s3-presigned-url.ts、READMEの手順、確認コマンド。
確認: npm test、aws s3 ls、aws s3 sync --dryrun の出力で説明してください。
参照: AWS CLI s3/sync docs と Anthropic Claude Code common workflows。
Policy जानबूझकर narrow है: GetObject सिर्फ assets, PutObject सिर्फ uploads, और ListBucket prefix से limited। Common mistake है ListBucket को object ARN पर लगाना या पूरे bucket को s3:* देना।
DeleteObject भी pitfall है। कई upload flows को delete की जरूरत नहीं होती। जरूरत हो तो अलग role या script रखें, dryrun और human confirmation के साथ। Claude Code JSON लिख सकता है, लेकिन final scope इंसान check करे।
aws s3 sync सुरक्षित तरीके से चलाएँ
aws s3 sync local path और S3 prefix compare करता है। —delete जोड़ने पर remote files delete हो सकती हैं। Static deploy में ठीक, backup या shared images में dangerous।
# Backup use case: append-only, no --delete.
BACKUP_DATE=$(date +%Y-%m-%d)
aws s3 sync ./backups "s3://${S3_BUCKET}/backups/${BACKUP_DATE}/" \
--region "${AWS_REGION}" \
--storage-class STANDARD_IA \
--exclude "*.tmp"
aws s3 ls "s3://${S3_BUCKET}/backups/${BACKUP_DATE}/" --recursive --summarize
मेरा pattern है dryrun पहले। Script bucket, region, prefix और diff print करे। Delete count ज्यादा हो तो stop करे। यह simple guard गलत folder plus —delete से बचाता है।
Presigned URL temporary permission है
Presigned URL bucket को public किए बिना limited time access देता है। Invoice PDF, private report, export file या user download में यह अच्छा pattern है।
तीन बातें याद रखें: expiresIn seconds में है, key prefix validate करें, और browser filename को direct S3 key न बनाएं। Server पर uploads/yyyy/mm/dd/uuid.ext जैसा key बनाएं। बहुत लंबा URL common failure है।
Cost, cache और public access pitfall
S3 cost केवल storage नहीं है। Requests, transfer, CloudFront, versioning और lifecycle भी cost बनाते हैं। Public assets के लिए CloudFront और cache-control रखें। Private files के लिए bucket public न करें।
DRYRUN_OUTPUT=$(aws s3 sync ./dist "s3://${S3_BUCKET}/site/" --delete --dryrun)
echo "$DRYRUN_OUTPUT"
DELETE_COUNT=$(echo "$DRYRUN_OUTPUT" | grep -c "delete:" || true)
if [ "$DELETE_COUNT" -gt 20 ]; then
echo "Too many deletes: ${DELETE_COUNT}. Stop and review."
exit 1
fi
कई पुराने tutorials S3 static website hosting और public bucket दिखाते हैं। Modern production में private S3 + CloudFront ज्यादा सुरक्षित और audit में explain करने योग्य होता है।
Claude Code क्या करे, human क्या review करे
Claude Code sync script, TypeScript helper, README, tests और error messages बना सकता है। Human AWS account, bucket, prefix, DeleteObject, public access और CloudFront check करे। Run से पहले aws sts get-caller-identity और aws s3 ls चलाएँ।
Masa verification note: prefix separation सबसे उपयोगी था। site, assets, uploads, backups और private-reports अलग करने से IAM review और prompt दोनों साफ हो गए। Claude Code से verification steps भी लिखवाने पर trust बढ़ता है।
Summary
Claude Code × AWS S3 शक्तिशाली है, लेकिन goal सिर्फ upload नहीं बल्कि safe operation है। use case अलग करें, sync dryrun से शुरू करें, broad DeleteObject से बचें, S3 private रखें और short presigned URL दें।
इसे real repository में लागू करने के लिए training / consultation.
मुफ़्त PDF: Claude Code cheatsheet
Email डालें और commands, review habits तथा safe workflow वाली एक-page PDF पाएँ.
हम आपका data सुरक्षित रखते हैं और spam नहीं भेजते.
लेखक के बारे में
Masa
Claude Code workflow और team adoption पर काम करने वाला engineer.
संबंधित लेख
Claude Code Free PDF Funnel Checklist: लेख ट्रैफिक को signup और product click में बदलना
Claude Code लेखों से मुफ्त PDF, Gumroad product और consultation तक पाठक को ले जाने की जांच।
Claude Code Obsidian to CLAUDE.md workflow: context बार-बार न समझाएं
Obsidian notes को CLAUDE.md operating notes में बदलकर Claude Code sessions को resume करना आसान बनाएं.
Claude Code Revenue CTA Routing: article से PDF, Gumroad और consultation तक
Reader intent के आधार पर free PDF, Gumroad products और consultation तक CTA route करने वाला workflow.