0

When I run this code:

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')

I get this error:

TypeError: can only concatenate str (not "dict") to str

With this stack trace:

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

Normally, that error just means you can't combine a dictionary with a string. That makes sense. But the only argument I'm passing here that's supposed to be a string, that's not a literal string itself, is bucket_name, and I'm sure that's a string because I did:

print(type(bucket_name))

... and it returned string.

And the body I don't think Body is causing any problems (per this example). Body is of type 'bytes,' according to the docs.

The stack trace makes me wonder if it's a bug with Boto3 itself... but I doubt it. What am I doing wrong?

David
  • 1,620
  • 3
  • 20
  • 39
  • 1
    Isn't the concatenation error happening at `k_date = self._sign(('AWS4' + key).encode('utf-8')`? Maybe `key` isn't a string? That would be the secret key. – Michael - sqlbot Dec 04 '18 at 08:08
  • YES! That was it! I had no idea what the key was, I figured it must have been one of the parameters I was passing into the method, since whenever I commented out that method call I wouldn't get the error. Thank you! Want to add that as an answer, and I'll mask it as the correct answer? – David Dec 04 '18 at 13:26

1 Answers1

0

It works perfectly well for me:

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')
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470