def post(self, *args): # Add entry into bucket and flag as multipart upload if self.bucket_name and self.object_name: bucket_name = self.bucket_name object_name = self.object_name else: bucket_name,object_name = args if bucket_name not in self._get_bucket_names(): self._error(code=404,s3code='NSB') return original_name = urllib.unquote(object_name) bucket_object = Binary(self.request.body) object_size = bucket_object.__len__() object_md5 = self._object_md5(bucket_object) if self.uploadId: # We have a multipart upload, so iterate over the parts to generate the md5 hash and calculate size # This is the last call made after the mutlipart upload with the uploadId mupmd5 = hashlib.md5() mupsize = 0 for mup in self.application.S3[bucket_name].find({'object_name':object_name}): mupmd5.update(mup['object']) mupsize += mup['size'] self.application.S3[bucket_name].insert_one({'object_name':object_name,'object':bucket_object,'multipart':True,'md5':mupmd5.hexdigest(),'size':mupsize,'added':datetime.datetime.utcnow(),'updated':datetime.datetime.utcnow(),}) self.render_xml({"InitiateMultipartUploadResult": { "Bucket": bucket_name, "Prefix": self.prefix, "Key":object_name, "UploadId":object_name }})
def put(self, *args): if self.bucket_name and self.object_name: bucket_name = self.bucket_name object_name = self.object_name else: bucket_name,object_name = args original_name = urllib.unquote(object_name) if bucket_name not in self._get_bucket_names(): self._error(code=404,s3code='NSB') return # Insert object and then calculate computed md5 of stored object, size, then update and return # If the object already exists, delete contents and add updated timestamp and update existance = self.application.S3[bucket_name].find({"object_name":original_name}) if existance.count() > 0 and self.partNumber == None: existance_id = existance.next()['_id'] update_object = Binary(self.request.body) object_size = update_object.__len__() object_md5 = self._object_md5(update_object) self.application.S3[bucket_name].update({"_id":existance_id},{'$set': {'object':update_object,'md5':object_md5,'updated':datetime.datetime.utcnow(),'size':object_size}}) self.set_header('etag', '"%s"' % object_md5) self.finish() return if self.partNumber: tobeinserted = {'object_name':original_name,'object':Binary(self.request.body),'partNumber':self.partNumber} else: tobeinserted = {'object_name':original_name,'object':Binary(self.request.body)} inserted_object_id = self.application.S3[bucket_name].insert_one(tobeinserted).inserted_id inserted_object = self._get_bucket_object(bucket_name=bucket_name,_id=inserted_object_id) object_size = inserted_object['object'].__len__() object_md5 = self._object_md5(inserted_object['object']) self.application.S3[bucket_name].update({'_id':inserted_object_id},{'$set': {'md5':object_md5,'updated':datetime.datetime.utcnow(),'added':datetime.datetime.utcnow(),'size':object_size}}) self.set_header('etag', '"%s"' % object_md5) self.finish()