使用Boto3客户端将对象放置到AWS S3存储桶时发生TypeError

时间:2018-12-04 03:01:53

标签: python amazon-web-services amazon-s3 boto3

当我运行此代码时:

bucket_name = self.myBucket['PhysicalResourceId']  #evaluates to 'myBucket234342'
some_binary_data = b'Here we have some data'
S3client.put_object(Body=some_binary_data, Bucket=bucket_name, Key='hello.txt')

我收到此错误:

  

TypeError:只能将str(而不是“ dict”)连接到str

使用此堆栈跟踪:

ERROR [0.333s]: test_cant_access_encrypted_files_unless_authorized (testS3Security.TestS3Security)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\ddrayton\Desktop\template-build-s3\tests\testS3Security.py", line 130, in test_cant_access_encrypted_files_unless_authorized
    S3client.put_object(Body=some_binary_data, Bucket=bucket_name, Key='helloencrypt.txt')
  File "C:\Users\ddrayton\AppData\Roaming\Python\Python37\site-packages\botocore\client.py", line 320, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "C:\Users\ddrayton\AppData\Roaming\Python\Python37\site-packages\botocore\client.py", line 610, in _make_api_call
    operation_model, request_dict)
  File "C:\Users\ddrayton\AppData\Roaming\Python\Python37\site-packages\botocore\endpoint.py", line 102, in make_request
    return self._send_request(request_dict, operation_model)
  File "C:\Users\ddrayton\AppData\Roaming\Python\Python37\site-packages\botocore\endpoint.py", line 132, in _send_request
    request = self.create_request(request_dict, operation_model)
  File "C:\Users\ddrayton\AppData\Roaming\Python\Python37\site-packages\botocore\endpoint.py", line 116, in create_request
    operation_name=operation_model.name)
  File "C:\Users\ddrayton\AppData\Roaming\Python\Python37\site-packages\botocore\hooks.py", line 356, in emit
    return self._emitter.emit(aliased_event_name, **kwargs)
  File "C:\Users\ddrayton\AppData\Roaming\Python\Python37\site-packages\botocore\hooks.py", line 228, in emit
    return self._emit(event_name, kwargs)
  File "C:\Users\ddrayton\AppData\Roaming\Python\Python37\site-packages\botocore\hooks.py", line 211, in _emit
    response = handler(**kwargs)
  File "C:\Users\ddrayton\AppData\Roaming\Python\Python37\site-packages\botocore\signers.py", line 90, in handler
    return self.sign(operation_name, request)
  File "C:\Users\ddrayton\AppData\Roaming\Python\Python37\site-packages\botocore\signers.py", line 157, in sign
    auth.add_auth(request)
  File "C:\Users\ddrayton\AppData\Roaming\Python\Python37\site-packages\botocore\auth.py", line 425, in add_auth
    super(S3SigV4Auth, self).add_auth(request)
  File "C:\Users\ddrayton\AppData\Roaming\Python\Python37\site-packages\botocore\auth.py", line 368, in add_auth
    signature = self.signature(string_to_sign, request)
  File "C:\Users\ddrayton\AppData\Roaming\Python\Python37\site-packages\botocore\auth.py", line 348, in signature
    k_date = self._sign(('AWS4' + key).encode('utf-8'),
TypeError: can only concatenate str (not "dict") to str

通常,该错误仅表示您无法将字典与字符串组合在一起。那讲得通。但是我在这里传递的唯一一个参数应该是字符串,而不是文字字符串本身,是bucket_name,而且我确定这是一个字符串,因为我这样做了:

print(type(bucket_name))

...,它返回了字符串。

我认为身体没有引起任何问题(per this example)。根据{{​​3}},主体的类型为“字节”。

堆栈跟踪让我怀疑这是否是Boto3本身的错误...但是我对此表示怀疑。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

对我来说效果很好:

import boto3

s3_client = boto3.client('s3', region_name='ap-southeast-2')

some_binary_data = b'Here we have some data'
s3_client.put_object(Body=some_binary_data, Bucket='my-bucket', Key='hello.txt')