devHọc Dev
Bài học

Tuần 5 - Ngày 4: S3 Advanced & Storage Gateway

Tuần 5 – Ngày 4

Mục tiêu học tập

  • Hiểu S3 advanced features
  • Nắm vững S3 Access Points và Object Lambda
  • Biết Storage Gateway use cases

1. S3 Access Points

S3ACCESSPOINTSProblem:Complexbucketpoliciesformulti-tenantSolution:S3BucketAccessPoint1AccessPoint2(FinanceTeam)(Analytics)prefix:/fin/prefix:/data/VPConlyPublicEachAccessPoint:-OwnDNSname-Ownaccesspolicy-Networkcontrols(VPCorpublic)

Multi-Region Access Points

MULTI-REGIONACCESSPOINTSSingleglobalendpointNearestbucketmrap.mrap.s3-global.amazonaws.comus-east-1eu-west-1ap-se-1bucketbucketbucket+S3Cross-RegionReplicationBenefits:-Automaticroutingtonearestbucket-Failoversupport-Replicationtimecontrol

2. S3 Object Lambda

S3OBJECTLAMBDATransformdataasit'sretrieved:ClientGETrequestObjectLambdaAccessPointLambdaFunction(transform)S3Bucket(originaldata)Usecases:-RedactPII-Convertformats(JSONtoXML)-Resizeimageson-the-fly-Addwatermarks

3. S3 Batch Operations

For bulk operations on billions of objects:

Supported Operations:
- Copy objects
- Invoke Lambda function
- Replace tags
- Replace ACL
- Restore from Glacier
- Object Lock retention
- Replication (S3 Batch Replication)

Input: S3 Inventory report or CSV manifest

4. Storage Gateway

Types

STORAGEGATEWAYTYPES1.S3FILEGATEWAYOn-premNFS/SMBGatewayS3FilesstoredasS3objects2.FSxFILEGATEWAYOn-premSMBGatewayFSxforWindowsWindowsfileshares3.VOLUMEGATEWAYCached:Hotdatalocal,fulldatainS3Stored:Fulldatalocal,asyncbackuptoS3Blockstorage(iSCSI)4.TAPEGATEWAYVirtualtapelibrarybackedbyS3/GlacierForbackupapplications

5. S3 Pre-signed URLs

S3PRE-SIGNEDURLClient(khôngcóAWScredentials)1.RequestURLAppServer/Lambda(cóIAMpermissions)2.SignURLvicredentialscamình(SigV4,kèmexpiration)Pre-signedURLClient3.GET/PUTtrctiếplênS3(khôngquaserver)S3Bucket(objectvnprivate)

Cơ chế: URL chứa chữ ký SigV4 + expiration → bất kỳ ai cầm URL đều GET/PUT được object đó không cần AWS credentials, trong thời hạn cho phép.

Điểm quan trọng cho exam:

  • URL thừa hưởng quyền của principal ký URL. Nếu người ký không có s3:GetObject trên object đó → URL tạo ra vẫn hợp lệ về mặt cú pháp nhưng vô dụng (Access Denied).
  • Thời hạn tối đa 7 ngày với SigV4 khi ký bằng IAM user credentials (long-term access keys).
  • Bẫy hay thi: ký bằng temporary credentials (IAM Role — EC2 instance profile, Lambda execution role, STS AssumeRole) → URL chết khi session token hết hạn, dù bạn đặt expiration dài hơn. Cần URL sống lâu → ký bằng IAM user credentials (hoặc gia hạn session).
  • Per-object: mỗi URL chỉ cho phép đúng 1 operation (GET hoặc PUT) trên đúng 1 object key.

Use cases:

  • Direct upload từ client: browser/mobile app PUT thẳng lên S3, bỏ qua app server → giảm tải, giảm chi phí bandwidth qua server.
  • Chia sẻ file private có thời hạn: gửi link download hết hạn sau X giờ mà không cần mở public bucket.

So sánh S3 Pre-signed URL vs CloudFront Signed URL/Cookies

Tiêu chíS3 Pre-signed URLCloudFront Signed URL/Cookies
Đường điTruy cập S3 trực tiếpQua CDN (edge caching, latency thấp)
OriginChỉ S3S3 hoặc custom origin (ALB, EC2, on-prem)
Phạm viPer-object, per-operation (GET/PUT)Signed URL: per-file; Signed Cookies: NHIỀU file (Resource pattern như /videos/*)
Quản lý keyKhông có key riêng — ký bằng IAM credentials, dễ tạo (SDK/CLI)Key groups (public/private key pair do bạn quản lý và rotate)
Điều kiệnChỉ expirationExpiration + giới hạn IP range, date range (custom policy)
UploadHỗ trợ PUT (direct upload)Chủ yếu cho phân phối content (download/streaming)

Exam keywords:

  • "serve private content through CDN", "video streaming nhiều file cho subscriber" → CloudFront Signed URL/Cookies (nhiều file → Signed Cookies)
  • "temporary direct upload/download to S3", "client upload không qua server" → S3 Pre-signed URL

6. CloudFront nâng cao cho SAP-C02

Origin Groups (Origin Failover)

CLOUDFRONTORIGINGROUP+S3CRRUsersCloudFrontDistributionOriginGroupPrimarySecondaryS3us-east-1S3eu-west-1failoveron5xx/timeoutS3CRRPrimaryfail(500/502/503/504,timeout)CloudFronttretryrequestsangsecondary
  • Origin Group = primary + secondary origin; CloudFront failover khi primary trả HTTP status codes cấu hình được (500, 502, 503, 504, và 403/404 nếu chọn) hoặc timeout/connection failure.
  • Pattern kinh điển SAP-C02: Origin Group + S3 Cross-Region Replication → origin HA đa region cho static content, không cần Route 53 failover, không cần thay đổi client.
  • Chỉ áp dụng cho GET/HEAD/OPTIONS (không failover cho PUT/POST).

Geo Restriction

  • Chặn/cho phép truy cập distribution theo quốc gia (GeoIP database) — cấu hình allowlist HOẶC blocklistdistribution level.
  • Use case: compliance/licensing — nội dung chỉ được phát hành ở một số quốc gia (bản quyền phim, quy định pháp lý).
  • Viewer bị chặn nhận HTTP 403 (có thể custom error page).
  • Phân biệt với Route 53 Geolocation routing: Route 53 route user đến endpoint khác nhau theo vị trí (vẫn được phục vụ, chỉ khác đích đến); CloudFront Geo Restriction chặn hẳn truy cập. "Block users from country X" → CloudFront Geo Restriction; "serve different content/endpoint per region" → Route 53 Geolocation.

Price Class

Price ClassEdge locationsTrade-off
AllToàn bộ (bao gồm South America, Australia...)Performance tốt nhất mọi nơi, chi phí cao nhất
200Hầu hết regions (trừ các region đắt nhất)Cân bằng
100Chỉ US, Canada, Europe (+ Israel)Rẻ nhất, users ngoài US/EU chịu latency cao hơn
  • Price Class chỉ giới hạn edge locations phục vụ content — users ở region bị loại vẫn truy cập được (routed đến edge xa hơn), chỉ chậm hơn.
  • Exam keyword: "reduce CloudFront cost", "users chủ yếu ở US/EU" → Price Class 100.

7. Câu hỏi ôn tập

  1. S3 Access Points giải quyết vấn đề gì?

    Xem đáp án

    S3 Access Points giải quyết bucket policy phức tạp khi nhiều teams/applications cần truy cập cùng bucket với different permissions. Thay vì một bucket policy khổng lồ: tạo Access Point cho mỗi use case với policy riêng (ví dụ: AP for analytics team, AP for app team, AP for data science). Mỗi AP có hostname riêng và policy riêng. Cũng hỗ trợ VPC restriction (AP chỉ accessible từ VPC cụ thể).

  2. Multi-Region Access Points hoạt động như thế nào?

    Xem đáp án

    MRAP cung cấp single global endpoint routing requests đến S3 bucket gần nhất (lowest latency). Cần S3 CRR (Cross-Region Replication) giữa các buckets để sync data. Khi request đến MRAP endpoint → AWS route đến bucket Region có lowest latency. Nếu Region fail → failover tự động đến next best Region. Dùng cho: global apps cần low-latency reads, active-active multi-region storage architecture.

  3. S3 Object Lambda dùng để làm gì?

    Xem đáp án

    S3 Object Lambda cho phép thêm transformation code vào S3 GET requests — không cần copy data. Lambda function intercept S3 request và transform response trước khi trả về client. Use cases: (1) Data redaction (mask PII theo user permissions), (2) Format conversion (CSV → JSON, resize images), (3) Add watermarks, (4) Decompress/uncompress on-the-fly. Không cần thay đổi storage — transform theo real-time request.

  4. 4 loại Storage Gateway là gì?

    Xem đáp án

    (1) S3 File Gateway: NFS/SMB file shares backed by S3 — on-premises apps access S3 objects as files, (2) FSx File Gateway: caching local cho FSx for Windows — SMB access on-premises với caching (chỉ SMB, không hỗ trợ NFS), (3) Volume Gateway: iSCSI block storage backed by S3 — Stored (local primary + S3 backup) hoặc Cached (S3 primary + local cache), (4) Tape Gateway: virtual tape library (VTL) backed by S3 Glacier — replace physical tapes cho backup software. Phù hợp cho hybrid cloud storage scenarios.

  5. Một Lambda function tạo pre-signed URL với expiration 3 ngày, nhưng URL chết sau vài giờ. Tại sao và fix như thế nào?

    Xem đáp án

    Lambda ký URL bằng temporary credentials từ execution role (STS session token) → URL hết hạn khi session token hết hạn (thường vài giờ), bất kể expiration đặt là bao nhiêu. Fix: ký URL bằng IAM user credentials (long-term access keys, lưu trong Secrets Manager) nếu cần URL sống đến 7 ngày (max của SigV4), hoặc thiết kế lại để client xin URL mới khi cần (URL ngắn hạn thường an toàn hơn). Đây là bẫy kinh điển trong exam về pre-signed URLs.

  6. Khi nào dùng CloudFront Signed Cookies thay vì S3 Pre-signed URL?

    Xem đáp án

    Dùng CloudFront Signed Cookies khi cần serve private content qua CDN và user cần truy cập NHIỀU file (ví dụ: video streaming HLS với hàng trăm segment files, khu vực member-only của website) — 1 cookie cover cả resource pattern (/videos/*), tận dụng edge caching, có thể giới hạn IP/date range qua custom policy, hoạt động với cả custom origin. Dùng S3 Pre-signed URL khi cần temporary direct access đến 1 object cụ thể trên S3 — đặc biệt là direct upload (PUT) từ client bỏ qua app server, hoặc chia sẻ 1 file download có thời hạn. Keyword "through CDN / nhiều file" → CloudFront signed; "direct upload / 1 object tạm thời" → pre-signed.

8. Bài tập thực hành

  1. S3 Access Point multi-tenant: tạo bucket với 2 prefix (/finance/, /analytics/), tạo 2 Access Points với policy giới hạn theo prefix và 1 AP restrict theo VPC. Verify bằng CLI (--endpoint qua AP alias) rằng mỗi AP chỉ đọc được prefix của mình.

  2. S3 Object Lambda redaction: tạo Object Lambda Access Point với Lambda function mask field email/số điện thoại trong file JSON khi GET. So sánh response qua AP thường vs Object Lambda AP — dữ liệu gốc trong bucket không đổi.

  3. S3 Batch Operations: bật S3 Inventory cho bucket có sẵn vài trăm objects, dùng inventory report làm manifest chạy Batch Operations job replace tags hàng loạt. Xem completion report để hiểu cách xử lý partial failure.


Tài liệu tham khảo chính thức


Ngày tiếp theo: Database Options