Coverage for pytest_recap/cloud.py: 70%

37 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-06-16 00:19 -0600

1import re 

2 

3 

4def upload_to_cloud(uri, data): 

5 if uri.startswith("s3://"): 

6 return _upload_to_s3(uri, data) 

7 elif uri.startswith("gs://"): 

8 return _upload_to_gcs(uri, data) 

9 elif uri.startswith("azure://") or uri.startswith("https://"): 

10 return _upload_to_azure(uri, data) 

11 else: 

12 raise ValueError(f"Unknown cloud URI scheme: {uri}") 

13 

14 

15def _upload_to_s3(uri, data): 

16 import boto3 

17 

18 m = re.match(r"s3://([^/]+)/(.+)", uri) 

19 if not m: 

20 raise ValueError(f"Invalid S3 URI: {uri}") 

21 bucket, key = m.groups() 

22 s3 = boto3.client("s3") 

23 s3.put_object(Bucket=bucket, Key=key, Body=data) 

24 

25 

26def _upload_to_gcs(uri, data): 

27 from google.cloud import storage 

28 

29 m = re.match(r"gs://([^/]+)/(.+)", uri) 

30 if not m: 

31 raise ValueError(f"Invalid GCS URI: {uri}") 

32 bucket_name, blob_name = m.groups() 

33 client = storage.Client() 

34 bucket = client.bucket(bucket_name) 

35 blob = bucket.blob(blob_name) 

36 blob.upload_from_string(data) 

37 

38 

39def _upload_to_azure(uri, data): 

40 from azure.storage.blob import BlobServiceClient 

41 

42 m = re.match(r"azure://([^/]+)/(.+)", uri) 

43 if not m: 

44 raise ValueError(f"Invalid Azure URI: {uri}") 

45 container, blob_name = m.groups() 

46 conn_str = None # Use default env var or config 

47 bsc = BlobServiceClient.from_connection_string(conn_str) if conn_str else BlobServiceClient() 

48 container_client = bsc.get_container_client(container) 

49 container_client.upload_blob(blob_name, data, overwrite=True)