def _build_query_string(self, params):
     keys = list(params.keys())
     keys.sort(key=lambda x: x.lower())
     pairs = []
     for key in keys:
         val = get_utf8able_str(params[key])
         pairs.append(key + '=' + self._escape_value(get_utf8able_str(val)))
     return '&'.join(pairs)
Exemple #2
0
    def create_bucket(self, bucket_name, headers=None,
                      location=Location.DEFAULT, policy=None,
                      storage_class='STANDARD'):
        """
        Creates a new bucket. By default it's located in the USA. You can
        pass Location.EU to create bucket in the EU. You can also pass
        a LocationConstraint for where the bucket should be located, and
        a StorageClass describing how the data should be stored.

        :type bucket_name: string
        :param bucket_name: The name of the new bucket.

        :type headers: dict
        :param headers: Additional headers to pass along with the request to GCS.

        :type location: :class:`boto.gs.connection.Location`
        :param location: The location of the new bucket.

        :type policy: :class:`boto.gs.acl.CannedACLStrings`
        :param policy: A canned ACL policy that will be applied to the new key
                       in GCS.

        :type storage_class: string
        :param storage_class: Either 'STANDARD' or 'DURABLE_REDUCED_AVAILABILITY'.

        """
        check_lowercase_bucketname(bucket_name)

        if policy:
            if headers:
                headers[self.provider.acl_header] = policy
            else:
                headers = {self.provider.acl_header : policy}
        if not location:
            location = Location.DEFAULT
        location_elem = ('<LocationConstraint>%s</LocationConstraint>'
                         % location)
        if storage_class:
            storage_class_elem = ('<StorageClass>%s</StorageClass>'
                                  % storage_class)
        else:
            storage_class_elem = ''
        data = ('<CreateBucketConfiguration>%s%s</CreateBucketConfiguration>'
                 % (location_elem, storage_class_elem))
        response = self.make_request(
            'PUT', get_utf8able_str(bucket_name), headers=headers,
            data=get_utf8able_str(data))
        body = response.read()
        if response.status == 409:
            raise self.provider.storage_create_error(
                response.status, response.reason, body)
        if response.status == 200:
            return self.bucket_class(self, bucket_name)
        else:
            raise self.provider.storage_response_error(
                response.status, response.reason, body)
    def tearDown(self):
        super(GsUtilUnitTestCase, self).tearDown()

        self.root_logger.handlers = self.log_handlers_save
        self.temp_log_handler.flush()
        self.temp_log_handler.close()
        self.log_handler_stream.seek(0)
        log_output = self.log_handler_stream.read()
        self.log_handler_stream.close()
        os.unlink(self.log_handler_file)

        sys.stdout.seek(0)
        sys.stderr.seek(0)
        if six.PY2:
            stdout = sys.stdout.read()
            stderr = sys.stderr.read()
        else:
            try:
                stdout = sys.stdout.read()
                stderr = sys.stderr.read()
            except UnicodeDecodeError:
                sys.stdout.seek(0)
                sys.stderr.seek(0)
                stdout = sys.stdout.buffer.read()
                stderr = sys.stderr.buffer.read()
        [six.ensure_text(string) for string in self.accumulated_stderr]
        [six.ensure_text(string) for string in self.accumulated_stdout]
        stdout = six.ensure_text(get_utf8able_str(stdout))
        stderr = six.ensure_text(get_utf8able_str(stderr))
        stdout += ''.join(self.accumulated_stdout)
        stderr += ''.join(self.accumulated_stderr)
        _AttemptToCloseSysFd(sys.stdout)
        _AttemptToCloseSysFd(sys.stderr)
        sys.stdout = self.stdout_save
        sys.stderr = self.stderr_save
        os.unlink(self.stdout_file)
        os.unlink(self.stderr_file)

        _id = six.ensure_text(self.id())
        if self.is_debugging and stdout:
            print_to_fd('==== stdout {} ====\n'.format(_id), file=sys.stderr)
            print_to_fd(stdout, file=sys.stderr)
            print_to_fd('==== end stdout ====\n', file=sys.stderr)
        if self.is_debugging and stderr:
            print_to_fd('==== stderr {} ====\n'.format(_id), file=sys.stderr)
            print_to_fd(stderr, file=sys.stderr)
            print_to_fd('==== end stderr ====\n', file=sys.stderr)
        if self.is_debugging and log_output:
            print_to_fd('==== log output {} ====\n'.format(_id),
                        file=sys.stderr)
            print_to_fd(log_output, file=sys.stderr)
            print_to_fd('==== end log output ====\n', file=sys.stderr)
 def build_auth_path(self, bucket, key=u''):
     bucket = six.ensure_text(bucket, encoding='utf-8')
     key = get_utf8able_str(key)
     path = u''
     if bucket != u'':
         path = u'/' + bucket
     return path + '/%s' % urllib.parse.quote(key)
Exemple #5
0
 def build_auth_path(self, bucket, key=''):
     key = get_utf8able_str(key)
     if isinstance(bucket, bytes):
         bucket = bucket.decode('utf-8')
     path = ''
     if bucket != '':
         path = '/' + bucket
     return path + '/%s' % urllib.parse.quote(key)
 def query_string(self, http_request):
     parameter_names = sorted(http_request.params.keys())
     pairs = []
     for pname in parameter_names:
         pval = get_utf8able_str(http_request.params[pname])
         pairs.append(
             urllib.parse.quote(pname, safe=''.encode('ascii')) + '=' +
             urllib.parse.quote(pval, safe='-_~'.encode('ascii')))
     return '&'.join(pairs)
 def canonical_query_string(self, http_request):
     # Note that we just do not return an empty string for
     # POST request. Query strings in url are included in canonical
     # query string.
     l = []
     for param in sorted(http_request.params):
         value = get_utf8able_str(http_request.params[param])
         l.append('%s=%s' % (urllib.parse.quote(
             param, safe='-_.~'), urllib.parse.quote(value, safe='-_.~')))
     return '&'.join(l)
 def canonical_query_string(self, http_request):
     # POST requests pass parameters in through the
     # http_request.body field.
     if http_request.method == 'POST':
         return ""
     l = []
     for param in sorted(http_request.params):
         value = get_utf8able_str(http_request.params[param])
         l.append('%s=%s' % (urllib.parse.quote(
             param, safe='-_.~'), urllib.parse.quote(value, safe='-_.~')))
     return '&'.join(l)
 def _calc_signature(self, params, *args):
     boto.log.debug('using _calc_signature_1')
     hmac = self._get_hmac()
     keys = list(params.keys())
     keys.sort(key=lambda x: x.lower())
     pairs = []
     for key in keys:
         hmac.update(key.encode('utf-8'))
         val = get_utf8able_str(params[key]).encode('utf-8')
         hmac.update(val)
         pairs.append(key + '=' + urllib.parse.quote(val))
     qs = '&'.join(pairs)
     return (qs, base64.b64encode(hmac.digest()))
 def _calc_signature(self, params, *args):
     boto.log.debug('using _calc_signature_0')
     hmac = self._get_hmac()
     s = params['Action'] + params['Timestamp']
     hmac.update(s.encode('utf-8'))
     keys = params.keys()
     keys.sort(cmp=lambda x, y: cmp(x.lower(), y.lower()))
     pairs = []
     for key in keys:
         val = get_utf8able_str(params[key])
         pairs.append(key + '=' + urllib.parse.quote(val))
     qs = '&'.join(pairs)
     return (qs, base64.b64encode(hmac.digest()))
 def _calc_signature(self, params, verb, path, server_name):
     boto.log.debug('using _calc_signature_2')
     string_to_sign = '%s\n%s\n%s\n' % (verb, server_name.lower(), path)
     hmac = self._get_hmac()
     params['SignatureMethod'] = self.algorithm()
     if self._provider.security_token:
         params['SecurityToken'] = self._provider.security_token
     keys = sorted(params.keys())
     pairs = []
     for key in keys:
         val = get_utf8able_str(params[key]).encode('utf-8')
         pairs.append(
             urllib.parse.quote(key, safe='') + '=' +
             urllib.parse.quote(val, safe='-_~'))
     qs = '&'.join(pairs)
     boto.log.debug('query string: %s' % qs)
     string_to_sign += qs
     boto.log.debug('string_to_sign: %s' % string_to_sign)
     hmac.update(string_to_sign.encode('utf-8'))
     b64 = base64.b64encode(hmac.digest())
     boto.log.debug('len(b64)=%d' % len(b64))
     boto.log.debug('base64 encoded digest: %s' % b64)
     return (qs, b64)
Exemple #12
0
 def build_path_base(self, bucket, key=''):
     key = get_utf8able_str(key)
     return '/%s' % urllib.parse.quote(key)
Exemple #13
0
 def build_path_base(self, bucket, key=''):
     key = get_utf8able_str(key)
     path_base = '/'
     if bucket:
         path_base += "%s/" % bucket
     return path_base + urllib.parse.quote(key)
Exemple #14
0
    def set_contents_from_string(self,
                                 s,
                                 headers=None,
                                 replace=True,
                                 cb=None,
                                 num_cb=10,
                                 policy=None,
                                 md5=None,
                                 if_generation=None):
        """
        Store an object in GCS using the name of the Key object as the
        key in GCS and the string 's' as the contents.
        See set_contents_from_file method for details about the
        parameters.

        :type headers: dict
        :param headers: Additional headers to pass along with the
                        request to AWS.

        :type replace: bool
        :param replace: If True, replaces the contents of the file if
                        it already exists.

        :type cb: function
        :param cb: a callback function that will be called to report
                   progress on the upload. The callback should accept
                   two integer parameters, the first representing the
                   number of bytes that have been successfully
                   transmitted to GCS and the second representing the
                   size of the to be transmitted object.

        :type cb: int
        :param num_cb: (optional) If a callback is specified with
                       the cb parameter this parameter determines the
                       granularity of the callback by defining
                       the maximum number of times the callback will
                       be called during the file transfer.

        :type policy: :class:`boto.gs.acl.CannedACLStrings`
        :param policy: A canned ACL policy that will be applied to the
                       new key in GCS.

        :type md5: A tuple containing the hexdigest version of the MD5
                   checksum of the file as the first element and the
                   Base64-encoded version of the plain checksum as the
                   second element. This is the same format returned by
                   the compute_md5 method.
        :param md5: If you need to compute the MD5 for any reason prior
                    to upload, it's silly to have to do it twice so this
                    param, if present, will be used as the MD5 values
                    of the file. Otherwise, the checksum will be computed.

        :type if_generation: int
        :param if_generation: (optional) If set to a generation number, the
            object will only be written to if its current generation number is
            this value. If set to the value 0, the object will only be written
            if it doesn't already exist.
        """

        # Clear out any previously computed md5 hashes, since we are setting the content.
        self.md5 = None
        self.base64md5 = None

        fp = StringIO(get_utf8able_str(s))
        r = self.set_contents_from_file(fp,
                                        headers,
                                        replace,
                                        cb,
                                        num_cb,
                                        policy,
                                        md5,
                                        if_generation=if_generation)
        fp.close()
        return r