我有以下 python 代码,它试图将文件上传到谷歌云存储桶,当我运行它时,我收到以下错误
"WindowsPath('sample.flac') could not be converted to unicode"
这是我的代码
from pathlib import Path
from google.cloud import storage
filename = Path(sys.argv[1])
remote_filename = filename.with_suffix('.flac')
def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print(' File {} uploaded to {}.'.format(
source_file_name,
destination_blob_name))
upload_blob('speech-demo-2',filename,remote_filename)
我不明白它出了什么问题。 而且我的 Python 知识充其量也只是初级的。我一周前才开始学习Python。
编辑:我包括完整的回溯
Traceback (most recent call last):
File "F:\cloud-speech-to-text\test.py", line 53, in <module>
upload_blob('speech-demo-2',filename,remote_filename)
File "F:\cloud-speech-to-text\test.py", line 21, in upload_blob
blob = bucket.blob(destination_blob_name)
File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\google\cloud\storage\bucket.py", line 379, in blob
kms_key_name=kms_key_name,
File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\google\cloud\storage\blob.py", line 164, in __init__
name = _bytes_to_unicode(name)
File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\google\cloud\_helpers.py", line 389, in _bytes_to_unicode
raise ValueError("%r could not be converted to unicode" % (value,))
ValueError: WindowsPath('sample.flac') could not be converted to unicode
--更新-- 尝试了snakecharmerb的方法,没有成功。这就是我得到的
Traceback (most recent call last):
File "F:\cloud-speech-to-text\test.py", line 53, in <module>
upload_blob('speech-demo-2',filename,remote_filename)
File "F:\cloud-speech-to-text\test.py", line 23, in upload_blob
blob.upload_from_filename(source_file_name)
File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\google\cloud\storage\blob.py", line 1127, in upload_from_filename
content_type = self._get_content_type(content_type, filename=filename)
File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\google\cloud\storage\blob.py", line 647, in _get_content_type
content_type, _ = mimetypes.guess_type(filename)
File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\mimetypes.py", line 291, in guess_type
return _db.guess_type(url, strict)
File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\mimetypes.py", line 116, in guess_type
scheme, url = urllib.parse.splittype(url)
File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\urllib\parse.py", line 956, in splittype
match = _typeprog.match(url)
TypeError: expected string or bytes-like object
最佳答案
bucket.blob
需要一个字符串作为 blob_name
参数,但您要向它传递一个 Path
实例 - 具体来说,是一个 WindowsPath
.
您可以通过调用 str
将路径转换为字符串。
>>> p = pathlib.Path('sample')
>>> fn = p.with_suffix('.flac')
>>> fn
WindowsPath('sample.flac')
>>> str(fn)
'sample.flac'
所以这应该有效:
blob = bucket.blob(str(destination_blob_name))
关于python - WindowsPath 无法转换为 unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54711053/