Amazon S3 (सिंपल स्टोरेज सर्विस) क्लाउड स्टोरेज की नींव है। प्रत्येक गंभीर AWS कार्यभार S3 का उपयोग करता है – स्थिर वेबसाइटों और बैकअप से लेकर डेटा लेक और ML प्रशिक्षण डेटासेट तक। यह संपूर्ण गाइड 2026 में एस3 को पहली बकेट से लेकर प्रोडक्शन-ग्रेड आर्किटेक्चर तक कवर करता है।
📋 Table of Contents
S3 क्या है?
S3 ऑब्जेक्ट स्टोरेज है: फ़ाइलें (ऑब्जेक्ट्स) बकेट में संग्रहीत होती हैं, जिन्हें HTTP के माध्यम से एक्सेस किया जाता है। महत्वपूर्ण अवधारणाएं:
- बाल्टी– वस्तुओं के लिए कंटेनर, विश्व स्तर पर अद्वितीय नाम
- वस्तु– फ़ाइल + मेटाडेटा, प्रत्येक 5TB तक
- Key– बकेट के भीतर ऑब्जेक्ट का पूरा पथ (उदाहरण के लिए,
images/2026/photo.jpg) - क्षेत्र– बकेट एक AWS क्षेत्र में रहता है
- सहनशीलता— 99.999999999% (11 नौ) — डेटा 3+ AZs में दोहराया गया है
बकेट बनाना और फ़ाइलें अपलोड करना
# AWS CLI setup
pip install awscli
aws configure # enter Access Key, Secret, region
# Create bucket
aws s3 mb s3://my-bucket-2026 --region us-east-1
# Upload file
aws s3 cp file.jpg s3://my-bucket-2026/images/file.jpg
# Upload directory
aws s3 sync ./dist s3://my-bucket-2026/website/ --delete
# List objects
aws s3 ls s3://my-bucket-2026/
aws s3 ls s3://my-bucket-2026/images/ --recursive
# Download
aws s3 cp s3://my-bucket-2026/data.csv ./data.csv
# Delete
aws s3 rm s3://my-bucket-2026/old-file.txt
aws s3 rm s3://my-bucket-2026/old-folder/ --recursive
Boto3 – S3 के लिए पायथन SDK
import boto3
from botocore.exceptions import ClientError
from pathlib import Path
import json
# Initialize client
s3 = boto3.client('s3', region_name='us-east-1')
# Or use resource (higher-level API)
s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket('my-bucket-2026')
# Create bucket
def create_bucket(bucket_name: str, region: str = 'us-east-1') -> bool:
try:
if region == 'us-east-1':
s3.create_bucket(Bucket=bucket_name)
else:
s3.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={'LocationConstraint': region}
)
return True
except ClientError as e:
print(f"Error: {e}")
return False
# Upload file
def upload_file(file_path: str, bucket: str, key: str = None) -> bool:
if key is None:
key = Path(file_path).name
try:
s3.upload_file(file_path, bucket, key,
ExtraArgs={'ContentType': 'image/jpeg'})
return True
except ClientError as e:
print(f"Upload failed: {e}")
return False
# Upload from memory (no temp file needed)
def upload_bytes(data: bytes, bucket: str, key: str, content_type: str = 'application/octet-stream'):
s3.put_object(Body=data, Bucket=bucket, Key=key, ContentType=content_type)
# Download
def download_file(bucket: str, key: str, dest_path: str):
s3.download_file(bucket, key, dest_path)
def download_bytes(bucket: str, key: str) -> bytes:
response = s3.get_object(Bucket=bucket, Key=key)
return response['Body'].read()
# List objects
def list_objects(bucket: str, prefix: str = '') -> list[dict]:
paginator = s3.get_paginator('list_objects_v2')
objects = []
for page in paginator.paginate(Bucket=bucket, Prefix=prefix):
objects.extend(page.get('Contents', []))
return objects
# Delete
def delete_object(bucket: str, key: str):
s3.delete_object(Bucket=bucket, Key=key)
स्टेटिक वेबसाइट होस्टिंग
# Enable static hosting
aws s3 website s3://my-website-bucket/ --index-document index.html --error-document 404.html
# Bucket policy for public access
cat > policy.json << 'EOF'
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-website-bucket/*"
}]
}
EOF
aws s3api put-bucket-policy --bucket my-website-bucket --policy file://policy.json
# Deploy website
aws s3 sync ./dist s3://my-website-bucket/ --delete --cache-control "max-age=31536000" --exclude "*.html"
# HTML files: no cache
aws s3 sync ./dist s3://my-website-bucket/ --exclude "*" --include "*.html" --cache-control "no-cache"
# Website URL: http://my-website-bucket.s3-website-us-east-1.amazonaws.com
पूर्व-हस्ताक्षरित यूआरएल – सुरक्षित अस्थायी पहुंच
# Generate time-limited URLs for private objects
def get_presigned_url(bucket: str, key: str, expiry_seconds: int = 3600) -> str:
url = s3.generate_presigned_url(
'get_object',
Params={'Bucket': bucket, 'Key': key},
ExpiresIn=expiry_seconds
)
return url
# Upload pre-signed URL — let clients upload directly to S3
def get_presigned_upload_url(bucket: str, key: str, content_type: str, expiry: int = 300) -> dict:
response = s3.generate_presigned_post(
bucket,
key,
Fields={'Content-Type': content_type},
Conditions=[
{'Content-Type': content_type},
['content-length-range', 1, 10 * 1024 * 1024] # max 10MB
],
ExpiresIn=expiry
)
return response
# FastAPI endpoint returning upload URL
# @app.get('/upload-url')
# def get_upload_url(filename: str, content_type: str):
# key = f'uploads/{uuid4()}/{filename}'
# url_data = get_presigned_upload_url(BUCKET, key, content_type)
# return {'url': url_data['url'], 'fields': url_data['fields'], 'key': key}
S3 संग्रहण वर्ग (लागत अनुकूलन)
| कक्षा | उदाहरण | लागत |
|---|---|---|
| मानक | बार-बार पहुँचे | $0.023/जीबी |
| बुद्धिमान-स्तरीय | अज्ञात पहुंच पैटर्न | ऑटो अनुकूलन |
| मानक-IA | दुर्लभ पहुंच | $0.0125/जीबी |
| ग्लेशियर तत्काल | पुरालेख, एमएस पुनर्प्राप्ति | $0.004/जीबी |
| ग्लेशियर डीप आर्काइव | अनुपालन पुरालेख | $0.00099/जीबी |
जीवनचक्र नीतियाँ – स्वचालित लागत बचत
{
"Rules": [{
"ID": "archive-old-logs",
"Status": "Enabled",
"Filter": {"Prefix": "logs/"},
"Transitions": [
{"Days": 30, "StorageClass": "STANDARD_IA"},
{"Days": 90, "StorageClass": "GLACIER"}
],
"Expiration": {"Days": 365}
}]
}
S3 संस्करण और प्रतिकृति
# Enable versioning
aws s3api put-bucket-versioning --bucket my-bucket --versioning-configuration Status=Enabled
# Cross-region replication (in AWS Console or CloudFormation)
# Requires versioning on both source and destination buckets
# List object versions
aws s3api list-object-versions --bucket my-bucket --prefix data.csv
सुरक्षा सर्वोत्तम अभ्यास
- सार्वजनिक पहुंच को अवरुद्ध करेंडिफ़ॉल्ट रूप से – केवल वही खोलें जिसकी आपको आवश्यकता है
- IAM भूमिकाओं का उपयोग करेंEC2/लैम्ब्डा एक्सेस के लिए – कोई हार्डकोडेड कुंजी नहीं
- सर्वर-साइड एन्क्रिप्शन सक्षम करें(एसएसई-एस3 या एसएसई-केएमएस)
- क्लाउडट्रेल सक्षम करें– सभी S3 API कॉल का ऑडिट करें
- वीपीसी एंडपॉइंट का उपयोग करेंEC2 से निजी पहुंच के लिए
- एमएफए डिलीट सक्षम करेंमहत्वपूर्ण डेटा वाले संस्करणित बकेट के लिए
S3 असीम रूप से स्केलेबल है, इसकी लागत प्रति जीबी पेनी है, और प्रत्येक AWS सेवा के साथ एकीकृत है। सीएलआई और बोटो3 में महारत हासिल करें, उपयोगकर्ता अपलोड के लिए पूर्व-हस्ताक्षरित यूआरएल का उपयोग करें, लागत अनुकूलन के लिए जीवनचक्र नीतियों का उपयोग करें, और हमेशा डिफ़ॉल्ट रूप से सार्वजनिक पहुंच को अवरुद्ध करें।
🔗 Share this article
✍️ Leave a Comment