Download scripts/s3.py from killbill007/test111: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/killbill007/test111/resolve/main/scripts/s3.py
- Command line
-
hf download hf://killbill007/test111/scripts/s3.py
-
curl -L -o s3.py https://huggingface.co/killbill007/test111/resolve/main/scripts/s3.py
1.53 kB
| import boto3 | |
| from botocore.exceptions import NoCredentialsError, PartialCredentialsError | |
| s3_client = boto3.client('s3') | |
| bucket = 'app-faceanimate-s3' | |
| #s3_client.list_objects_v2(Bucket=bucket)['Contents'] | |
| def upload_to_s3(bucket_name: str, local_file_path: str, s3_key: str) -> bool: | |
| s3_client = boto3.client('s3') | |
| try: | |
| s3_client.upload_file(local_file_path, bucket_name, s3_key) | |
| print(f"Successfully uploaded {local_file_path} to {bucket_name}/{s3_key}") | |
| return True | |
| except Exception as e: | |
| print(f"Error uploading file: {e}") | |
| return False | |
| def generate_presigned_url(bucket_name: str, object_key: str, expiration: int = 3600) -> str: | |
| s3_client = boto3.client('s3') | |
| try: | |
| response = s3_client.generate_presigned_url( | |
| 'get_object', | |
| Params={'Bucket': bucket_name, 'Key': object_key}, | |
| ExpiresIn=expiration | |
| ) | |
| return response | |
| except (NoCredentialsError, PartialCredentialsError) as e: | |
| print(f"Credentials error: {e}") | |
| return None | |
| except Exception as e: | |
| print(f"Error generating pre-signed URL: {e}") | |
| return None | |
| def delete_from_s3(bucket_name: str, object_key: str) -> bool: | |
| s3_client = boto3.client('s3') | |
| try: | |
| s3_client.delete_object(Bucket=bucket_name, Key=object_key) | |
| print(f"Successfully deleted {object_key} from {bucket_name}") | |
| return True | |
| except Exception as e: | |
| print(f"Error deleting file: {e}") | |
| return False | |