diff options
Diffstat (limited to 'fbin/file_storage/s3.py')
-rw-r--r-- | fbin/file_storage/s3.py | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/fbin/file_storage/s3.py b/fbin/file_storage/s3.py index a11488f..e2dd1ea 100644 --- a/fbin/file_storage/s3.py +++ b/fbin/file_storage/s3.py @@ -21,7 +21,7 @@ class Storage(BaseStorage): key += '_thumb' return key - def store_file(self, uploaded_file, file_hash, user, ip): + def upload_file(self, uploaded_file, file_hash, user): bucket = self.client.Bucket(self.app.config['S3_BUCKET']) key = self._get_object_key(file_hash, user.id if user else 0) obj = bucket.upload_fileobj(Fileobj=uploaded_file.stream, Key=key) @@ -29,12 +29,28 @@ class Storage(BaseStorage): if not size: obj = self.client.ObjectSummary(self.app.config['S3_BUCKET'], key) size = obj.size + return size + + def store_file(self, uploaded_file, file_hash, user, ip): + size = self.upload_file(uploaded_file, file_hash, user) try: return self.add_file(file_hash, uploaded_file.filename, size, user, ip) except: obj.delete() raise + def file_exists(self, f): + key = self.get_object_key(f) + bucket = self.app.config['S3_BUCKET'] + obj = self.client.Object(bucket, key) + try: + meta = obj.load() + return True + except botocore.exceptions.ClientError as e: + if e.response['Error']['Code'] == '404': + return False + raise + def get_file(self, f, thumb=False): key = self.get_object_key(f, thumb=thumb) if thumb: |