def get_key(self, key_name, validate=True): """ Returns a Key instance for an object in this bucket. :type key_name: string :param key_name: The name of the key to retrieve :rtype: :class:'ssg.SSGInterface.key.SSGKey' :returns: A Key object from this bucket. """ method = "GET" url = "buckets" key = None response, content = self.connection.make_request(method, url, bucket=self.name, key=key_name) try: utils.check_response(response) key = self._get_key_internal(self.key_class(self, key_name), content) except request_error as re: print "in get_key : " + str(content) if not validate is True: self.connection.request_error_handler(re, 'get_key', method, url, response, bucket=self.name, key=key_name) key = SSGKey(self, name=key_name) return key
def _finish_response(self): method = "POST" url = "buckets" bucket_name = self.bucket.name query = "/key_finish?path=" + quote(self.name) request_param = { "storage_path_s3": self.real_name, "bucket_s3": self.bucket.real_name } response, content = self.bucket.connection.make_request( method, url, body=request_param, bucket=bucket_name, query=query) try: utils.check_response(response) data = json.loads(content) #데이터 형식 확인 후 key_list return except request_error as re: self.bucket.connection.request_error_handler(re, 'finish_response', method, url, response, bucket=bucket_name, query=query) return True
def get_key(self, key_name, validate=True): """ Returns a Key instance for an object in this bucket. :type key_name: string :param key_name: The name of the key to retrieve :type validate: boolean :param validate: If True, it doesn't raise request_error if there is no tree. (Default: True) :rtype: :class:'ssg.SSGInterface.key.SSGKey' :returns: A Key object from this bucket. """ method = "GET" url = "buckets" key = None response, content = self.connection.make_request(method, url, bucket=self.name, key=key_name) try: utils.check_response(response) key = self._get_key_internal(self.key_class(self, key_name), content) except request_error as re: print "in get_key : " + str(content) if not validate is True: self.connection.request_error_handler(re, 'get_key', method, url, response, bucket=self.name, key=key_name) key = SSGKey(self, name=key_name) return key
def create_bucket(self, bucket_name, location=Location.DEFAULT): """ create bucket by bucket_name. return user's real bucket name :type bucket_name: string :param bucket_name: The name of the new bucket :type location: str :param location: The location of the new bucket. You can use one of the constants in :class: ssg.SSGInterface.connection.Location """ method = "POST" url = 'buckets' request_param = {'name': bucket_name, 'group_key': self.group_key} bucket = None if not location == Location.DEFAULT: request_param.update({'location': location}) response, content = self.make_request(method, url, body=request_param) try: utils.check_response(response) data = json.loads(content) real_name = self._get_real_name(data) bucket = self.bucket_class(self, bucket_name, real_name) print bucket_name+" is made success." except request_error as re: self.request_error_handler(re, 'create_bucket', method, url, response, bucket=bucket_name) return bucket
def set_contents_from_filename(self, filename=None, size_s3=None, size_ssg=None): """ Store an object in SSG using the name of the Key object as the key in SSG. :type filename: string :param filename: The path of upload file. :type size_s3: int :param size_s3: The size in bytes, of s3 file size :type size_ssg: int :param size_ssg: The size in bytes, of ssg file size :rtype: int :return: The number of bytes written to the key. """ method = "POST" url = "buckets" self.filepath = filename if self.filepath is None: self.size_ssg = size_ssg else: self.size_ssg = path.getsize(self.filepath) self.size_s3 = size_s3 self.size = self.size_s3 + self.size_ssg self.filename = filename request_param = { 'name': self.name, 'size': self.size, 'size_s3': self.size_s3, 'size_ssg': self.size_ssg } response, content = self.bucket.connection.make_request( method, url, bucket=self.bucket.name, key=self.name, body=request_param) try: utils.check_response(response) self.set_metadata(content) except request_error as re: self.bucket.connection.request_error_handler( re, 'set_contents_from_filename', method, url, response, bucket=self.bucket.name, key=self.name) return self.size_ssg
def get_contents_to_filename(self, filename): method = "GET" url = self.url response, content = self.bucket.connection.make_request(method, url, file_io=True) try: utils.check_response(response) with open(filename, 'wb') as f: f.write(content) except request_error as re: self.bucket.connection.request_error_handler(re, 'get_contents_to_filename', method, url, response)
def get_all_buckets(self): method = "GET" url = "keys" query = "/bucket" response, content = self.make_request(method, url, group_key=self.group_key, query=query) bucket_list = [] try: utils.check_response(response) bucket_list = self._make_bucket_list(content) except request_error as re: self.request_error_handler(re, 'get_all_bucket', method, url, response, group_key=self.group_key) return bucket_list
def connection(self, group_key, bucket_class=SSGBucket): self.group_key = group_key method = "POST" url = "key_tokens" param = {"group_key": self.group_key} response, content = self.make_request(method, url, body=param) try: utils.check_response(response) data = json.loads(content) self._set_token(data) except request_error as re: self.request_error_handler(re, 'connection', method, url, response) self.bucket_class = bucket_class
def list(self): key_list = [] method = "GET" url = "buckets" query = '/key_list?path=/' response, content = self.connection.make_request(method, url, bucket=self.name, query=query) try: utils.check_response(response) #데이터 형식 확인 후 key_list return key_list = utils.make_select_list(content, "path") except request_error as re: self.connection.request_error_handler(re, 'get_key_list', method, url, response, bucket=self.name) return key_list
def get_bucket(self, bucket_name, validate=True): method = "GET" url = "buckets" bucket = None response, content = self.make_request(method, url, bucket=bucket_name) try: utils.check_response(response) data = json.loads(content) real_name = self._get_real_name(data) bucket = self.bucket_class(self, bucket_name, real_name) except request_error as re: if not validate is True: self.request_error_handler(re, 'get_bucket', method, url, response, bucket=bucket_name) bucket = self.bucket_class(self, bucket_name) return bucket
def _finish_response(self): method = "POST" url = "buckets" bucket_name = self.bucket.name query = "/key_finish?path="+quote(self.name) request_param = {"storage_path_s3": self.real_name, "bucket_s3": self.bucket.real_name} print str(type(self.real_name)) + " " + str(type(self.bucket.real_name)) response, content = self.bucket.connection.make_request(method, url, body=request_param, bucket=bucket_name, query=query) try: utils.check_response(response) data = json.loads(content) #데이터 형식 확인 후 key_list return print 'data : ' + str(data) except request_error as re: self.bucket.connection.request_error_handler(re, 'finish_response', method, url, response, bucket=bucket_name, query=query) return True
def delete(self): """ Delete this key from S3 """ method = "DELETE" url = "buckets" response, content = self.bucket.connection.make_request( method, url, bucket=self.bucket.name, key=self.name) try: utils.check_response(response) except request_error as re: self.bucket.connection.request_error_handler( re, 'delete_key', method, url, response, bucket=self.bucket.name, key=self.name)
def get_contents_to_filename(self, filename): """ Retrieve an object from SSSG using the name of the Key object as the key in SSG. Write the contents of the object to the file name to by 'filename'. :type filename: string :param filename: The name of download file. """ method = "GET" url = self.url response, content = self.bucket.connection.make_request(method, url, file_io=True) try: utils.check_response(response) with open(filename, 'wb') as f: f.write(content) except request_error as re: self.bucket.connection.request_error_handler( re, 'get_contents_to_filename', method, url, response)
def create_bucket(self, bucket_name, location=Location.DEFAULT): """ create bucket by bucket_name. return user's real bucket name :type bucket_name: string :param bucket_name: The name of the new bucket :type location: str :param location: The location of the new bucket. You can use one of the constants in :class: ssg.SSGInterface.connection.Location """ method = "POST" url = 'buckets' request_param = {'name': bucket_name, 'group_key': self.group_key} bucket = None if not location == Location.DEFAULT: request_param.update({'location': location}) response, content = self.make_request(method, url, body=request_param) try: utils.check_response(response) data = json.loads(content) real_name = self._get_real_name(data) bucket = self.bucket_class(self, bucket_name, real_name) print bucket_name + " is made success." except request_error as re: self.request_error_handler(re, 'create_bucket', method, url, response, bucket=bucket_name) return bucket
def list(self): """ List keys' name within a bucket. this will return a list of available keys' name within the bucket :rtype: list :return: a list of all available keys' name within the bucket . """ key_list = [] method = "GET" url = "buckets" query = '/key_list?path=/' response, content = self.connection.make_request(method, url, bucket=self.name, query=query) try: utils.check_response(response) #데이터 형식 확인 후 key_list return key_list = utils.make_select_list(content, "path") except request_error as re: self.connection.request_error_handler(re, 'get_key_list', method, url, response, bucket=self.name) return key_list
def get_bucket(self, bucket_name, validate=True): """ Retrieves a bucket by name. If the bucket does not exist, an ``SS3_Request_error`` will be raised. If you are unsure if the bucket exists or not, you can use the ``S3Connection.lookup`` method, which will either return a valid bucket or ``None``. :type bucket_name: string :param bucket_name: The name of the bucket :type validate: boolean :param validate: If True, it will try to verify the bucket exists on the service-side. (Default: False) """ method = "GET" url = "buckets" bucket = None response, content = self.make_request(method, url, bucket=bucket_name) try: utils.check_response(response) data = json.loads(content) real_name = self._get_real_name(data) bucket = self.bucket_class(self, bucket_name, real_name) except request_error as re: if not validate is True: self.request_error_handler(re, 'get_bucket', method, url, response, bucket=bucket_name) bucket = self.bucket_class(self, bucket_name) return bucket
def set_contents_from_filename(self, filename, size_s3): method = "POST" url = "buckets" self.filepath = filename self.size_ssg = path.getsize(self.filepath) self.size_s3 = size_s3 self.size = self.size_s3 + self.size_ssg self.filename = filename request_param = {'name': self.name, 'size': self.size, 'size_s3': self.size_s3, 'size_ssg': self.size_ssg} response, content = self.bucket.connection.make_request(method, url, bucket=self.bucket.name, key=self.name, body=request_param) try: utils.check_response(response) self.set_metadata(content) except request_error as re: self.bucket.connection.request_error_handler(re, 'set_contents_from_filename', method, url, response, bucket=self.bucket.name, key=self.name) return self.size