Boto:上传到Amazon S3 OK,但上传到Google Cloud Storage:'str'不支持缓冲区界面

时间:2015-07-08 08:02:19

标签: amazon-s3 google-cloud-storage boto

我使用几乎完全相同的代码分别使用boto将文件上传到Amazon S3和Google Cloud Storage:

import boto

filename = 'abc.png'
filenameWithPath = os.path.dirname(os.path.realpath(__file__)) + '/' + filename
cloudFilename = 'uploads/' + filename

# Upload to Amazon S3
conn = boto.connect_s3(aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET_KEY)
bucket = conn.get_bucket(AWS_BUCKET_NAME)
fpic = boto.s3.key.Key(bucket)
fpic.key = cloudFilename
fpic.set_contents_from_filename(filenameWithPath)

# Upload to Google Cloud Storage
conn = boto.connect_gs(gs_access_key_id=GS_ACCESS_KEY, gs_secret_access_key=GS_SECRET_KEY)
bucket = conn.get_bucket(GS_BUCKET_NAME)
fpic = boto.s3.key.Key(bucket)
fpic.key = cloudFilename
fpic.set_contents_from_filename(filenameWithPath)

Amazon S3部分代码运行完美。但是,Google云端存储部分会在语句TypeError, 'str' does not support the buffer interface上提供错误消息fpic.set_contents_from_filename(...)

问题是什么?

1 个答案:

答案 0 :(得分:0)

Request Method: POST
Request URL: http://localhost:8000/upload/

Traceback:
File "<folder of virtual environ>/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "<folder of my project>/upload/views.py" in upload
  59.         fpic.set_contents_from_filename(filenameWithPath)
File "<folder of virtual environ>/lib/python3.4/site-packages/boto/s3/key.py" in set_contents_from_filename
  1362.                                                encrypt_key=encrypt_key)
File "<folder of virtual environ>/lib/python3.4/site-packages/boto/s3/key.py" in set_contents_from_file
  1293.                            chunked_transfer=chunked_transfer, size=size)
File "<folder of virtual environ>/lib/python3.4/site-packages/boto/s3/key.py" in send_file
  750.                                  chunked_transfer=chunked_transfer, size=size)
File "<folder of virtual environ>/lib/python3.4/site-packages/boto/s3/key.py" in _send_file_internal
  951.             query_args=query_args
File "<folder of virtual environ>/lib/python3.4/site-packages/boto/s3/connection.py" in make_request
  664.             retry_handler=retry_handler
File "<folder of virtual environ>/lib/python3.4/site-packages/boto/connection.py" in make_request
  1071.                           retry_handler=retry_handler)
File "<folder of virtual environ>/lib/python3.4/site-packages/boto/connection.py" in _mexe
  940.                                       request.body, request.headers)
File "<folder of virtual environ>/lib/python3.4/site-packages/boto/s3/key.py" in sender
  840.                     http_conn.send('%x;\r\n' % chunk_len)
File "/usr/lib/python3.4/http/client.py" in send
  876.                     self.sock.sendall(d)
File "/usr/lib/python3.4/ssl.py" in sendall
  718.                 v = self.send(data[count:])
File "/usr/lib/python3.4/ssl.py" in send
  679.                 v = self._sslobj.write(data)

Exception Type: TypeError at /upload/
Exception Value: 'str' does not support the buffer interface