AWS S3 File Management
Store, organize, and retrieve files at scale with Amazon S3.
A Simple Analogy
AWS S3 is like a massive, organized warehouse. You can store any file, organize it with labels (tags), control who accesses it, and retrieve it instantly from anywhere. You pay only for what you store.
What Is AWS S3?
Amazon S3 (Simple Storage Service) is object storage for files of any size. It's durable, highly available, and scales infinitely.
Core Concepts
| Concept | Meaning | |---------|---------| | Bucket | Top-level container (like folder) | | Object | File stored in bucket | | Key | File path/name (photos/pic1.jpg) | | ACL | Access control list | | Lifecycle | Rules for object expiration |
.NET Usage with AWS SDK
using Amazon.S3;
using Amazon.S3.Model;
var s3Client = new AmazonS3Client();
// Upload file
var putRequest = new PutObjectRequest
{
BucketName = "my-bucket",
Key = "uploads/document.pdf",
FilePath = "/path/to/document.pdf",
ContentType = "application/pdf"
};
await s3Client.PutObjectAsync(putRequest);
// Download file
var getRequest = new GetObjectRequest
{
BucketName = "my-bucket",
Key = "uploads/document.pdf"
};
using var response = await s3Client.GetObjectAsync(getRequest);
await response.ResponseStream.CopyToAsync(File.Create("local.pdf"));
// Delete file
await s3Client.DeleteObjectAsync("my-bucket", "uploads/document.pdf");
// Generate presigned URL (temporary download link)
var urlRequest = new GetPreSignedUrlRequest
{
BucketName = "my-bucket",
Key = "uploads/document.pdf",
Expires = DateTime.UtcNow.AddHours(1)
};
var url = s3Client.GetPreSignedURL(urlRequest);
Practical Example
public class FileService
{
private readonly IAmazonS3 _s3Client;
private readonly string _bucketName = "my-app-storage";
public async Task<string> UploadFileAsync(IFormFile file, string userId)
{
var key = $"user-uploads/{userId}/{Guid.NewGuid()}-{file.FileName}";
using (var stream = file.OpenReadStream())
{
var putRequest = new PutObjectRequest
{
BucketName = _bucketName,
Key = key,
InputStream = stream,
ContentType = file.ContentType
};
await _s3Client.PutObjectAsync(putRequest);
}
return key;
}
public async Task<string> GetDownloadUrlAsync(string key)
{
var request = new GetPreSignedUrlRequest
{
BucketName = _bucketName,
Key = key,
Expires = DateTime.UtcNow.AddHours(1)
};
return _s3Client.GetPreSignedURL(request);
}
}
Best Practices
- Use versioning: Track file history
- Enable encryption: Protect data at rest
- Set lifecycle policies: Archive old files
- Use presigned URLs: Temporary access without AWS credentials
- Monitor costs: Track storage usage
Related Concepts
- CloudFront CDN for distribution
- S3 event notifications
- S3 lifecycle policies
- Server access logging
- Cross-region replication
Summary
AWS S3 provides scalable, durable object storage. Master bucket organization, access control, and presigned URLs to build secure file management systems.