|
1 | 1 | import os |
2 | 2 | import sys |
| 3 | +from typing import Optional |
3 | 4 | import boto3 |
| 5 | +import hashlib |
4 | 6 | import json |
5 | 7 | from pathlib import Path |
6 | 8 | from botocore.exceptions import ClientError |
7 | 9 |
|
8 | | -def upload_to_s3(file_path: Path, bucket_name: str, folder_name: str): |
9 | | - """Upload one file to the given S3 bucket under folder_name/.""" |
| 10 | +def _metadata(spec_hash: Optional[str], temporary: Optional[bool] = False): |
| 11 | + metadata = {} |
| 12 | + proxygen_version = '453743-sha907aa22b' |
| 13 | + if spec_hash is not None: |
| 14 | + metadata["spec_hash"] = spec_hash |
| 15 | + |
| 16 | + if temporary is not None: |
| 17 | + metadata["temporary"] = str(temporary) |
| 18 | + |
| 19 | + if proxygen_version is not None: |
| 20 | + metadata["proxygen_version"] = proxygen_version |
| 21 | + |
| 22 | + return metadata or None |
| 23 | + |
| 24 | +def upload_to_s3(file_path: Path, bucket_name: str, folder_name: str, temporary: bool = False): |
| 25 | + """ |
| 26 | + Upload a file to S3 with: |
| 27 | + - ACL bucket-owner-full-control |
| 28 | + - metadata including MD5 hash + temporary flag + proxygen_version |
| 29 | + """ |
10 | 30 | s3 = boto3.client("s3") |
11 | | - key = f"apis/{folder_name}/{file_path}" |
12 | 31 |
|
13 | | - try: |
| 32 | + # S3 key |
| 33 | + key = f"apis/{folder_name}/{file_path.name}" |
| 34 | + |
| 35 | + # --- Compute MD5 Hash for Metadata --- |
| 36 | + spec_json = file_path.read_text(encoding="utf-8") |
| 37 | + spec_hash = hashlib.md5(spec_json.encode("utf-8")).hexdigest() |
| 38 | + |
| 39 | + # Build complete metadata |
| 40 | + metadata = _metadata(spec_hash=spec_hash, temporary=temporary) |
| 41 | + |
| 42 | + # Build ExtraArgs |
| 43 | + extra_args = { |
| 44 | + "ACL": "bucket-owner-full-control" |
| 45 | + } |
| 46 | + |
| 47 | + if metadata: |
| 48 | + # Metadata must be lowercase keys, values must be strings |
| 49 | + extra_args["Metadata"] = metadata |
| 50 | + |
| 51 | + try: |
14 | 52 | s3.upload_file( |
15 | | - str(file_path), |
16 | | - bucket_name, |
17 | | - key, |
18 | | - ExtraArgs={'ACL': 'bucket-owner-full-control'} |
| 53 | + Filename=str(file_path), |
| 54 | + Bucket=bucket_name, |
| 55 | + Key=key, |
| 56 | + ExtraArgs=extra_args |
19 | 57 | ) |
20 | | - print(f"[OK] Uploaded → s3://{bucket_name}/apis/{key}") |
| 58 | + print(f"[OK] Uploaded → s3://{bucket_name}/{key}") |
| 59 | + print(f"[META] {metadata}") |
| 60 | + |
21 | 61 | except ClientError as e: |
22 | 62 | print(f"[ERROR] Upload failed: {file_path} → s3://{bucket_name}/{key}") |
23 | 63 | print(e) |
24 | 64 | sys.exit(1) |
25 | 65 |
|
26 | | - |
27 | 66 | def main(bucket_name: str, repo_name: str): |
28 | 67 | cwd = os.getcwd() |
29 | 68 | print("Current working directory:", cwd) |
|
0 commit comments