11import logging
22import os
3+ import boto3
4+ from botocore .exceptions import ClientError
35import pecan
46from pecan import response
57from pecan .secure import secure
@@ -89,7 +91,7 @@ def index_post(self):
8991 if request .POST .get ('force' , False ) is False :
9092 error ('/errors/invalid' , 'resource already exists and "force" key was not used' )
9193
92- full_path = self .save_file (file_obj )
94+ full_path , size = self .save_file (file_obj )
9395
9496 if self .binary is None :
9597 path = full_path
@@ -102,14 +104,19 @@ def index_post(self):
102104 self .binary = Binary (
103105 self .binary_name , self .project , arch = arch ,
104106 distro = distro , distro_version = distro_version ,
105- ref = ref , sha1 = sha1 , path = path , size = os . path . getsize ( path )
107+ ref = ref , sha1 = sha1 , path = path , size = size
106108 )
107109 else :
108110 self .binary .path = full_path
109111
110112 # check if this binary is interesting for other configured projects,
111113 # and if so, then mark those other repos so that they can be re-built
112114 self .mark_related_repos ()
115+
116+ # Remove the local file after S3 upload
117+ if pecan .conf .storage_method == 's3' :
118+ os .remove (full_path )
119+
113120 return dict ()
114121
115122 def mark_related_repos (self ):
@@ -175,8 +182,21 @@ def save_file(self, file_obj):
175182 for chunk in file_iterable :
176183 f .write (chunk )
177184
185+ if pecan .conf .storage_method == 's3' :
186+ bucket = pecan .conf .bucket
187+ object_name = os .path .basename (self .binary_name )
188+
189+ s3_client = boto3 .client ('s3' )
190+ try :
191+ with open (destination , 'rb' ) as f :
192+ s3_client .upload_fileobj (f , bucket , object_name )
193+ except ClientError as e :
194+ error ('/errors/error/' , 'file object upload to S3 failed with error %s' % e )
195+
196+ size = os .path .getsize (destination )
197+
178198 # return the full path to the saved object:
179- return destination
199+ return destination , size
180200
181201 @expose ()
182202 def _lookup (self , name , * remainder ):
0 commit comments