s3_interface.py
1.49 KB
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 boto3
import json
S3 = boto3.client('s3')
BUCKET = '2018-dcloud'
def list_path(bucket, user, path):
files = []
# get list
objects = S3.list_objects(Bucket=bucket, Prefix='{}/{}'.format(user, path), Delimiter='/')
# get sub directorys
common_prefixes = objects.get('CommonPrefixes')
if common_prefixes:
for obj in common_prefixes:
files.append({'type':'diretory', 'name':obj.get('Prefix').split('/')[-2]})
# get files
contents = objects.get('Contents')
if contents:
for obj in contents:
file = obj.get('Key').split('/')[-1]
if file != '':
files.append({'type':'file', 'name':file})
return {'files':files}
def upload_file(bucket, user, local_path, key):
return S3.upload_file(local_path, bucket, user+"/"+key)
def download_file(bucket, user, local_path, key):
return S3.download_file(bucket, user+"/"+key, local_path)
def delete_path(bucket, user, path):
return S3.delete_object(Bucket=bucket, Key=user+"/"+path)
def make_directory(bucket, user, path):
return S3.put_object(Bucket=BUCKET, Key=user+"/"+path)
#
def move_file(bucket, user, old_path, new_path):
S3.copy_object(Bucket=bucket, CopySource=bucket+"/"+user+"/"+old_path, Key=user+"/"+new_path)
S3.delete_object(Bucket=bucket, Key=user+"/"+old_path)
return
def copy_file(bucket, user, old_path, new_path):
S3.copy_object(Bucket=bucket, CopySource=bucket+"/"+user+"/"+old_path, Key=user+"/"+new_path)
return