File size: 1,528 Bytes
a0d1776
 
 
 
 
1b82d4c
 
 
a0d1776
 
1b82d4c
 
a0d1776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# This script `storage.py` is used to handle the cloud storage.
#   `upload_file`:
#   `list_all_files`:
#   `download_file`:

import os
import boto3

access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')
bucket_name = "hf-storage"

if (access_key_id is not None) and (secret_access_key is not None):
    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)


    def upload_file(file_name, target_name=None):
        if target_name is None:
            target_name = file_name
        try:
            s3.meta.client.upload_file(Filename=file_name, Bucket=bucket_name, Key=target_name)
            print(f"The file {file_name} has been uploaded!")
        except:
            print("Uploading failed!")

    def list_all_files():
        return [obj.key for obj in bucket.objects.all()]

    def download_file(file_name):
        ''' Download `file_name` from the bucket. todo:check existence before downloading!
        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.
        '''
        try:
            s3.meta.client.download_file(Bucket=bucket_name, Key=file_name, Filename=file_name)
            print(f"The file {file_name} has been downloaded!")
        except:
            print("Uploading failed!")