Skip to content

Commit bd33c74

Browse files
authored
Merge pull request #14 from MadeByMads/multipart
Changed self.s3_client in init to boto3.resource
2 parents 5dcda60 + 7b79d99 commit bd33c74

File tree

4 files changed

+100
-28
lines changed

4 files changed

+100
-28
lines changed

examples/google_drive_main.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from logging import debug
2+
from fastapi_cloud_drives import GoogleDrive
3+
from fastapi_cloud_drives import GoogleDriveConfig
4+
5+
from fastapi import FastAPI
6+
from fastapi.responses import JSONResponse
7+
import os
8+
app = FastAPI()
9+
10+
google_conf = {
11+
"CLIENT_ID_JSON" : "client_id.json",
12+
"SCOPES": [
13+
"https://www.googleapis.com/auth/drive"
14+
],
15+
# "STORAGE_JSON": "/home/sabuhi/opt/fastapi-cloud-drives/storage.json"
16+
}
17+
18+
config = GoogleDriveConfig(**google_conf)
19+
20+
gdrive = GoogleDrive(config)
21+
22+
@app.get("/list_files")
23+
async def list_files():
24+
f = await gdrive.list_files()
25+
return JSONResponse(status_code=200, content=f)
26+
27+
@app.get("/upload_file")
28+
async def upload_file():
29+
resp = await gdrive.upload_file(
30+
filename = "photo.jpg",
31+
filepath = "files/photo.jpg",
32+
)
33+
return JSONResponse(status_code=200, content=resp)
34+
35+
@app.get("/create_folder")
36+
async def create_folder():
37+
resp = await gdrive.create_folder(folder_name="Examples")
38+
return JSONResponse(status_code=200, content=resp)
39+
40+
@app.get("/download_file")
41+
async def download_file():
42+
r = await gdrive.download_file(file_name = "photo.jpeg")
43+
return JSONResponse(status_code=200, content=r)

fastapi_cloud_drives/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from fastapi_cloud_drives.fastapi_google import GoogleDrive
22
from fastapi_cloud_drives.config import GoogleDriveConfig
33

4+
from fastapi_cloud_drives.fastapi_s3 import S3
5+
46
__author__ = "[email protected]"
57

68
__all__ = [

fastapi_cloud_drives/fastapi_s3.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,13 @@
88
from botocore.exceptions import ClientError
99

1010

11-
class S3(CloudStorageAbstractClass):
12-
def __init__(self, conf):
11+
class S3:
12+
def __init__(self, conf:dict):
13+
self.session = boto3.session.Session()
1314
self.s3_client = boto3.resource("s3")
14-
self.region = None
15+
self.region = conf.get("region") or None
1516

16-
def auth(self):
17-
pass
18-
19-
def build_service(self):
20-
pass
21-
22-
def list_files(self):
23-
pass
24-
25-
def upload_file(self, file_name, bucket, object_name=None, extra_args: dict=None):
17+
async def upload_file(self, file_name: str, bucket: str, object_name=None, extra_args: dict=None):
2618
"""Upload a file to an S3 bucket
2719
2820
:param file_name: File to upload
@@ -33,16 +25,18 @@ def upload_file(self, file_name, bucket, object_name=None, extra_args: dict=None
3325
3426
:return: True if file was uploaded, else False
3527
"""
28+
s3_client = boto3.client('s3')
29+
3630
# If S3 object_name was not specified, use file_name
3731
if object_name is None:
3832
object_name = file_name
3933

4034
# Upload the file
4135
try:
42-
response = self.s3_client.upload_file(
43-
file_name = file_name,
44-
bucket = bucket,
45-
object_name = object_name,
36+
response = s3_client.upload_file(
37+
file_name,
38+
bucket,
39+
object_name,
4640
ExtraArgs = extra_args,
4741
Callback=ProgressPercentage(file_name)
4842
)
@@ -52,28 +46,31 @@ def upload_file(self, file_name, bucket, object_name=None, extra_args: dict=None
5246
return False
5347
return True
5448

55-
def download_file(self, bucket_name: str, file_name: str, object_name: str=None, extra_args: dict=None):
56-
self.s3_clients3.download_file(
49+
async def download_file(self, bucket_name: str, file_name: str, object_name: str=None, extra_args: dict=None):
50+
s3_client = self.session.resource("s3")
51+
52+
s3_client.download_file(
5753
bucket_name,
5854
file_name,
5955
object_name,
6056
ExtraArgs = extra_args,
6157
Callback=ProgressPercentage(file_name)
6258
)
6359

64-
def list_buckets(self):
60+
async def list_buckets(self):
6561
"""[List existing buckets]
6662
6763
Returns:
6864
[dict]: [List of buckets]
6965
"""
66+
s3_client = self.session.resource("s3")
67+
7068
buckets = []
71-
for bucket in self.s3_client.buckets.all():
72-
buckets.append(bucket)
73-
print(bucket.name)
69+
for bucket in s3_client.buckets.all():
70+
buckets.append(bucket.name)
7471
return buckets
7572

76-
def create_bucket(self, bucket_name: str, region_name: str = None):
73+
async def create_bucket(self, bucket_name: str):
7774
"""Create an S3 bucket in a specified region
7875
7976
If a region is not specified, the bucket is created in the S3 default
@@ -87,14 +84,15 @@ def create_bucket(self, bucket_name: str, region_name: str = None):
8784
:return: True if bucket created, else False
8885
"""
8986
# Create bucket
87+
s3_client = self.session.resource("s3")
9088
try:
9189
if self.region is None:
92-
self.s3_client.create_bucket(Bucket=bucket_name)
90+
s3_client.create_bucket(Bucket=bucket_name)
9391
else:
94-
self.s3_client = boto3.client('s3', region_name=region_name)
95-
location = {'LocationConstraint': region_name}
92+
s3_client = boto3.client('s3', region_name=self.region)
93+
location = {'LocationConstraint': self.region}
9694

97-
self.s3_client.create_bucket(
95+
s3_client.create_bucket(
9896
Bucket=bucket_name,
9997
CreateBucketConfiguration=location
10098
)

s3_main.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from logging import debug
2+
from fastapi_cloud_drives import S3
3+
4+
from fastapi import FastAPI
5+
from fastapi.responses import JSONResponse
6+
7+
app = FastAPI()
8+
9+
s3_conf = {
10+
"region": "eu-central-1",
11+
}
12+
13+
s3_client = S3(s3_conf)
14+
15+
16+
@app.get("/list_buckets")
17+
async def list_buckets():
18+
buckets = await s3_client.list_buckets()
19+
return JSONResponse(status_code=200, content=buckets)
20+
21+
@app.get("/create_bucket")
22+
async def create_bucket():
23+
created = await s3_client.create_bucket(bucket_name="fastapibucket")
24+
return JSONResponse(status_code=200, content=created)
25+
26+
@app.get("/upload_file")
27+
async def upload_file():
28+
created = await s3_client.upload_file(bucket="fastapibucket", file_name="files/pi.dmg")
29+
return JSONResponse(status_code=200, content=created)

0 commit comments

Comments
 (0)