Skip to content

Commit

Permalink
Migrating from Google Cloud Storage to S3 FILE upload
Browse files Browse the repository at this point in the history
  • Loading branch information
sibb committed Jan 26, 2019
1 parent dab6655 commit 8d3afae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
5 changes: 3 additions & 2 deletions controllers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def get(self, ngo_url):
ong_folder = security.hash_password(ngo.key.id(), "md5")
path = "{0}/{1}/{2}".format(USER_UPLOADS_FOLDER, str(ong_folder), filename)

file_url = CloudStorage.save_file(pdf, path)
file_url = CloudStorage.save_file_S3(pdf, path)

# close the file after it has been uploaded
pdf.close()
Expand Down Expand Up @@ -131,7 +131,8 @@ def post(self):
# output a hex string
filename = "{0}/{1}/{2}".format(USER_UPLOADS_FOLDER, str(user_folder), sha1( datetime.now().isoformat() ).hexdigest())

file_url = CloudStorage.save_file(a_file, filename)
# file_url = CloudStorage.save_file(a_file, filename)
file_url = CloudStorage.save_file_S3(a_file, filename)

if file_url:
file_urls.append( file_url )
Expand Down
28 changes: 27 additions & 1 deletion models/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@

from logging import info


#for pdf file server S3
import boto3
import sys
import boto3.s3.key


aws_access_key_id = "AKIAJSKWPCNXLQR5XP4A"
aws_secret_access_key = "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"
region = "eu-west-1"
bucket_name = "myBucket"

class CloudStorage(object):
"""docstring for CloudStorage"""

Expand Down Expand Up @@ -75,7 +87,21 @@ def save_file(file_to_save=None, filename=None):

return file_url


@staticmethod
def save_file_S3(file_to_save=None, filename=None):
# return "s3 file url"
file_url = ''
session = boto3.session.Session()
s3_resource = session.resource('s3')
current_region = session.region_name
try:
data = open(filename, 'rb')
saved_file = s3_resource.Bucket(bucket_name).put_object(ACL="public-read", Key=filename, Body=data)
data.close()
file_url = "https://" + bucket_name + ".s3.amazonaws.com/" + saved_file.key
except Exception as e:
info("<p>Error: %s</p>" %e)
return file_url

def read_file(self, filename):

Expand Down

0 comments on commit 8d3afae

Please sign in to comment.