File size: 1,539 Bytes
1b82d4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import boto3
import hashlib

access_key_id = os.environ['AWS_ACCESS_KEY_ID']
secret_access_key = os.environ['AWS_SECRET_ACCESS_KEY']
bucket_name = "hf-storage"

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!")

def hash_name(title, description):
    '''
    For same title and description, it should return the same value.
    '''
    name = title + description
    name = name.lower()
    md5 = hashlib.md5()
    md5.update(name.encode('utf-8'))
    hashed_string = md5.hexdigest()
    return hashed_string