Spaces:
Running
Running
File size: 1,759 Bytes
a0d1776 ae495a3 a0d1776 ae495a3 a0d1776 ae495a3 a0d1776 1b82d4c ae495a3 1b82d4c ae495a3 a0d1776 ae495a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# This script `storage.py` is used to handle the cloud storage.
# `upload_file`:
# Function to upload a local file to the specified S3 bucket.
# If the target_name is not specified, it will use the file_name as the object key.
# `list_all_files`:
# Function to list all the files in the specified S3 bucket.
# `download_file`:
# Function to download a file from the specified S3 bucket to the local machine using the specified file_name.
import os
import boto3
BUCKET_NAME = "hf-storage"
def get_client():
access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')
session = boto3.Session(
aws_access_key_id=access_key_id,
aws_secret_access_key=secret_access_key,
)
s3 = session.resource('s3')
bucket = s3.Bucket(BUCKET_NAME)
return s3, bucket
def upload_file(file_name, target_name=None):
s3, _ = get_client()
if target_name is None:
target_name = file_name
s3.meta.client.upload_file(Filename=file_name, Bucket=BUCKET_NAME, Key=target_name)
print(f"The file {file_name} has been uploaded!")
def list_all_files():
_, bucket = get_client()
return [obj.key for obj in bucket.objects.all()]
def download_file(file_name):
''' Download `file_name` from the bucket.
Bucket (str) – The name of the bucket to download from.
Key (str) – The name of the key to download from.
Filename (str) – The path to the file to download to.
'''
s3, _ = get_client()
s3.meta.client.download_file(Bucket=BUCKET_NAME, Key=file_name, Filename=file_name)
print(f"The file {file_name} has been downloaded!")
if __name__ == "__main__":
file = "sample-output.pdf"
upload_file(file)
|