def generate_presigned_url(self,
                               request_dict,
                               operation_name,
                               expires_in=3600,
                               region_name=None,
                               signing_name=None):
        """Generates a presigned url

        :type request_dict: dict
        :param request_dict: The prepared request dictionary returned by
            ``ibm_botocore.awsrequest.prepare_request_dict()``

        :type operation_name: str
        :param operation_name: The operation being signed.

        :type expires_in: int
        :param expires_in: The number of seconds the presigned url is valid
            for. By default it expires in an hour (3600 seconds)

        :type region_name: string
        :param region_name: The region name to sign the presigned url.

        :type signing_name: str
        :param signing_name: The name to use for the service when signing.

        :returns: The presigned url
        """
        request = create_request_object(request_dict)
        self.sign(operation_name, request, region_name, 'presign-url',
                  expires_in, signing_name)

        request.prepare()
        return request.url
 def test_create_request_object(self):
     request = create_request_object(self.request_dict)
     self.assertEqual(request.method, self.request_dict['method'])
     self.assertEqual(request.url, self.request_dict['url'])
     self.assertEqual(request.data, self.request_dict['body'])
     self.assertEqual(request.context, self.request_dict['context'])
     self.assertIn('User-Agent', request.headers)
 def test_enable_disable_callbacks_only_ever_registered_once(self):
     body = SignalTransferringBody()
     request = create_request_object({
         'method': 'PUT',
         'url': 'https://s3.amazonaws.com',
         'body': body,
         'headers': {},
         'context': {}
     })
     # Create two TransferManager's using the same client
     TransferManager(self.client)
     TransferManager(self.client)
     self.client.meta.events.emit('request-created.s3',
                                  request=request,
                                  operation_name='PutObject')
     # The client should have only have the enable/disable callback
     # handlers registered once depite being used for two different
     # TransferManagers.
     self.assertEqual(
         body.signal_transferring_call_count, 1,
         'The enable_callback() should have only ever been registered once')
     self.assertEqual(
         body.signal_not_transferring_call_count, 1,
         'The disable_callback() should have only ever been registered '
         'once')
示例#4
0
 def create_request(self, params, operation_model=None):
     request = create_request_object(params)
     if operation_model:
         event_name = 'request-created.{endpoint_prefix}.{op_name}'.format(
             endpoint_prefix=self._endpoint_prefix,
             op_name=operation_model.name)
         self._event_emitter.emit(event_name, request=request,
                                  operation_name=operation_model.name)
     prepared_request = self.prepare_request(request)
     return prepared_request
示例#5
0
 def create_request(self, params, operation_model=None):
     request = create_request_object(params)
     if operation_model:
         request.stream_output = any([
             operation_model.has_streaming_output,
             operation_model.has_event_stream_output
         ])
         service_id = operation_model.service_model.service_id.hyphenize()
         event_name = 'request-created.{service_id}.{op_name}'.format(
             service_id=service_id, op_name=operation_model.name)
         self._event_emitter.emit(event_name,
                                  request=request,
                                  operation_name=operation_model.name)
     prepared_request = self.prepare_request(request)
     return prepared_request
    def generate_presigned_post(self,
                                request_dict,
                                fields=None,
                                conditions=None,
                                expires_in=3600,
                                region_name=None):
        """Generates the url and the form fields used for a presigned s3 post

        :type request_dict: dict
        :param request_dict: The prepared request dictionary returned by
            ``ibm_botocore.awsrequest.prepare_request_dict()``

        :type fields: dict
        :param fields: A dictionary of prefilled form fields to build on top
            of.

        :type conditions: list
        :param conditions: A list of conditions to include in the policy. Each
            element can be either a list or a structure. For example:
            [
             {"acl": "public-read"},
             {"bucket": "mybucket"},
             ["starts-with", "$key", "mykey"]
            ]

        :type expires_in: int
        :param expires_in: The number of seconds the presigned post is valid
            for.

        :type region_name: string
        :param region_name: The region name to sign the presigned post to.

        :rtype: dict
        :returns: A dictionary with two elements: ``url`` and ``fields``.
            Url is the url to post to. Fields is a dictionary filled with
            the form fields and respective values to use when submitting the
            post. For example:

            {'url': 'https://mybucket.s3.amazonaws.com
             'fields': {'acl': 'public-read',
                        'key': 'mykey',
                        'signature': 'mysignature',
                        'policy': 'mybase64 encoded policy'}
            }
        """
        if fields is None:
            fields = {}

        if conditions is None:
            conditions = []

        # Create the policy for the post.
        policy = {}

        # Create an expiration date for the policy
        datetime_now = datetime.datetime.utcnow()
        expire_date = datetime_now + datetime.timedelta(seconds=expires_in)
        policy['expiration'] = expire_date.strftime(ibm_botocore.auth.ISO8601)

        # Append all of the conditions that the user supplied.
        policy['conditions'] = []
        for condition in conditions:
            policy['conditions'].append(condition)

        # Store the policy and the fields in the request for signing
        request = create_request_object(request_dict)
        request.context['s3-presign-post-fields'] = fields
        request.context['s3-presign-post-policy'] = policy

        self._request_signer.sign('PutObject', request, region_name,
                                  'presign-post')
        # Return the url and the fields for th form to post.
        return {'url': request.url, 'fields': fields}