206 lines
5.3 KiB
Markdown
206 lines
5.3 KiB
Markdown
# AWS S3 Direct Access Test Guide
|
|
# Hướng Dẫn Test Truy Cập Trực Tiếp AWS S3
|
|
|
|
## Overview / Tổng quan
|
|
|
|
Dự án này bao gồm các file test để kiểm tra kết nối và truy cập AWS S3 với credentials có quyền hạn chế.
|
|
|
|
## Files / Các File
|
|
|
|
### 1. `test_s3_direct_access.py`
|
|
File test cơ bản cho AWS S3, yêu cầu quyền `ListAllMyBuckets`.
|
|
|
|
**Sử dụng:**
|
|
```bash
|
|
python test_s3_direct_access.py
|
|
```
|
|
|
|
**Lưu ý:** File này sẽ báo lỗi `AccessDenied` nếu credentials không có quyền list tất cả buckets.
|
|
|
|
### 2. `test_s3_datacube_access.py` ⭐ (Recommended / Khuyên dùng)
|
|
File test nâng cao, truy cập trực tiếp các bucket cụ thể mà không cần quyền `ListAllMyBuckets`.
|
|
|
|
**Sử dụng:**
|
|
```bash
|
|
python test_s3_datacube_access.py
|
|
```
|
|
|
|
**Tính năng:**
|
|
- ✓ Load credentials từ `train_files/crediential.txt`
|
|
- ✓ Cấu hình datacube S3 access
|
|
- ✓ Test truy cập nhiều bucket phổ biến
|
|
- ✓ Hiển thị danh sách file trong bucket
|
|
- ✓ Thống kê kết quả test
|
|
|
|
## Kết quả Test / Test Results
|
|
|
|
### Các bucket có thể truy cập:
|
|
- ✅ **sentinel-cogs** (us-west-2) - Sentinel-2 L2A COGs data
|
|
- Public bucket chứa dữ liệu Sentinel-2
|
|
- Prefix: `sentinel-s2-l2a-cogs/`
|
|
|
|
### Credentials hiện tại:
|
|
```
|
|
Role: arn:aws:sts::876569415057:assumed-role/easi-asia-csiro-easihub-client/hienm2523001
|
|
Region: ap-southeast-1
|
|
Expiration: Token có thời hạn (session token)
|
|
```
|
|
|
|
### Quyền hạn (Permissions):
|
|
- ✅ Read objects từ public buckets
|
|
- ✅ List objects trong bucket cụ thể
|
|
- ✅ Head bucket (check bucket existence)
|
|
- ❌ ListAllMyBuckets (list tất cả buckets)
|
|
- ❌ GetBucketLocation (một số bucket)
|
|
|
|
## Cách sử dụng trong code / How to use in code
|
|
|
|
### 1. Load credentials và cấu hình S3:
|
|
|
|
```python
|
|
import os
|
|
from datacube.utils.rio import configure_s3_access
|
|
|
|
# Load credentials
|
|
os.environ['AWS_ACCESS_KEY_ID'] = "YOUR_ACCESS_KEY"
|
|
os.environ['AWS_SECRET_ACCESS_KEY'] = "YOUR_SECRET_KEY"
|
|
os.environ['AWS_SESSION_TOKEN'] = "YOUR_SESSION_TOKEN"
|
|
|
|
# Configure S3 access for datacube/rasterio
|
|
configure_s3_access(
|
|
aws_unsigned=False,
|
|
region_name='ap-southeast-1',
|
|
cloud_defaults=True
|
|
)
|
|
```
|
|
|
|
### 2. Truy cập S3 objects với boto3:
|
|
|
|
```python
|
|
import boto3
|
|
|
|
# Create S3 client
|
|
s3_client = boto3.client('s3', region_name='us-west-2')
|
|
|
|
# List objects in bucket
|
|
response = s3_client.list_objects_v2(
|
|
Bucket='sentinel-cogs',
|
|
Prefix='sentinel-s2-l2a-cogs/',
|
|
MaxKeys=10
|
|
)
|
|
|
|
for obj in response.get('Contents', []):
|
|
print(f"File: {obj['Key']}, Size: {obj['Size']} bytes")
|
|
```
|
|
|
|
### 3. Đọc dữ liệu từ S3 với rasterio:
|
|
|
|
```python
|
|
import rasterio
|
|
|
|
# Read raster file directly from S3
|
|
s3_path = 's3://sentinel-cogs/sentinel-s2-l2a-cogs/1/C/CV/2018/10/S2B_1CCV_20181004_0_L2A/B02.tif'
|
|
|
|
with rasterio.open(s3_path) as src:
|
|
data = src.read(1)
|
|
print(f"Shape: {data.shape}")
|
|
print(f"CRS: {src.crs}")
|
|
```
|
|
|
|
### 4. Load dữ liệu với xarray:
|
|
|
|
```python
|
|
import xarray as xr
|
|
import rioxarray
|
|
|
|
# Open S3 raster with rioxarray
|
|
s3_path = 's3://sentinel-cogs/sentinel-s2-l2a-cogs/1/C/CV/2018/10/S2B_1CCV_20181004_0_L2A/B02.tif'
|
|
ds = rioxarray.open_rasterio(s3_path)
|
|
|
|
print(ds)
|
|
```
|
|
|
|
## Refresh Credentials / Làm mới Credentials
|
|
|
|
AWS session tokens có thời hạn. Khi token hết hạn, bạn sẽ thấy lỗi:
|
|
```
|
|
ExpiredToken: The security token included in the request is expired
|
|
```
|
|
|
|
**Cách làm mới:**
|
|
1. Login lại vào AWS console hoặc EASI hub
|
|
2. Copy credentials mới
|
|
3. Update file `train_files/crediential.txt`
|
|
4. Chạy lại test
|
|
|
|
## Troubleshooting / Xử lý lỗi
|
|
|
|
### Lỗi: `ModuleNotFoundError: No module named 'boto3'`
|
|
```bash
|
|
pip install boto3 botocore
|
|
```
|
|
|
|
### Lỗi: `ModuleNotFoundError: No module named 'datacube'`
|
|
```bash
|
|
pip install datacube
|
|
```
|
|
|
|
### Lỗi: `AccessDenied`
|
|
- Kiểm tra credentials có đúng không
|
|
- Kiểm tra token còn hạn không
|
|
- Thử bucket khác (có thể bucket đó yêu cầu quyền cao hơn)
|
|
|
|
### Lỗi: `ExpiredToken`
|
|
- Token AWS đã hết hạn
|
|
- Cần refresh credentials mới
|
|
|
|
### Lỗi: `404 Not Found`
|
|
- Bucket không tồn tại
|
|
- Bucket name có thể sai
|
|
- Region có thể sai
|
|
|
|
## Dependencies / Thư viện cần thiết
|
|
|
|
```bash
|
|
pip install boto3 botocore datacube rasterio rioxarray xarray
|
|
```
|
|
|
|
Hoặc sử dụng file requirements:
|
|
```bash
|
|
pip install -r requirements_api.txt
|
|
```
|
|
|
|
## Security / Bảo mật
|
|
|
|
⚠️ **QUAN TRỌNG:**
|
|
- **KHÔNG** commit file `crediential.txt` lên Git
|
|
- **KHÔNG** share credentials công khai
|
|
- Session tokens có thời hạn ngắn (thường vài giờ)
|
|
- Luôn sử dụng IAM roles với quyền tối thiểu cần thiết
|
|
|
|
## Thông tin thêm / Additional Information
|
|
|
|
### Public Sentinel-2 Buckets:
|
|
- `sentinel-cogs` (us-west-2) - ✅ Accessible
|
|
- `sentinel-s2-l2a` (eu-central-1) - COGs format
|
|
- `sentinel-s2-l1c` (eu-central-1) - Level 1C
|
|
|
|
### EASI/Datacube Buckets:
|
|
- Thường là private buckets
|
|
- Cần credentials với quyền cụ thể
|
|
- Contact admin để được cấp quyền
|
|
|
|
## Examples / Ví dụ
|
|
|
|
Xem các file trong thư mục `backup_S3_download_Amazon/`:
|
|
- `new_import_S3.py` - Load dữ liệu S3 với datacube
|
|
|
|
## Contact / Liên hệ
|
|
|
|
Nếu cần thêm quyền truy cập hoặc gặp vấn đề:
|
|
- Contact: EASI Asia CSIRO admin
|
|
- Role: easi-asia-csiro-easihub-client
|
|
|
|
---
|
|
**Last updated:** March 4, 2026
|