Skip to content

Commit 4142ec4

Browse files
committed
Updated script to add additional metadata.
1 parent 59f178c commit 4142ec4

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

scripts/copy_spec.py

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,68 @@
11
import os
22
import sys
3+
from typing import Optional
34
import boto3
5+
import hashlib
46
import json
57
from pathlib import Path
68
from botocore.exceptions import ClientError
79

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+
"""
1030
s3 = boto3.client("s3")
11-
key = f"apis/{folder_name}/{file_path}"
1231

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:
1452
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
1957
)
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+
2161
except ClientError as e:
2262
print(f"[ERROR] Upload failed: {file_path} → s3://{bucket_name}/{key}")
2363
print(e)
2464
sys.exit(1)
2565

26-
2766
def main(bucket_name: str, repo_name: str):
2867
cwd = os.getcwd()
2968
print("Current working directory:", cwd)

0 commit comments

Comments
 (0)