I have a similar issues like How to upload a bytes image on Google Cloud Storage from a Python script.
我有类似的问题,如如何从Python脚本上传Google Cloud Storage上的字节图像。
I tried this
我试过这个
from google.cloud import storage
import cv2
from tempfile import TemporaryFile
import google.auth
credentials, project = google.auth.default()
client = storage.Client()
# https://console.cloud.google.com/storage/browser/[bucket-id]/
bucket = client.get_bucket('document')
# Then do other things...
image=cv2.imread('/Users/santhoshdc/Documents/Realtest/15.jpg')
with TemporaryFile() as gcs_image:
image.tofile(gcs_image)
blob = bucket.get_blob(gcs_image)
print(blob.download_as_string())
blob.upload_from_string('New contents!')
blob2 = bucket.blob('document/operations/15.png')
blob2.upload_from_filename(filename='gcs_image')
This is the error that's posing up
这是错误的结果
> Traceback (most recent call last): File
> "/Users/santhoshdc/Documents/ImageShapeSize/imageGcloudStorageUpload.py",
> line 13, in <module>
> blob = bucket.get_blob(gcs_image) File "/Users/santhoshdc/.virtualenvs/test/lib/python3.6/site-packages/google/cloud/storage/bucket.py",
> line 388, in get_blob
> **kwargs) File "/Users/santhoshdc/.virtualenvs/test/lib/python3.6/site-packages/google/cloud/storage/blob.py",
> line 151, in __init__
> name = _bytes_to_unicode(name) File "/Users/santhoshdc/.virtualenvs/test/lib/python3.6/site-packages/google/cloud/_helpers.py",
> line 377, in _bytes_to_unicode
> raise ValueError('%r could not be converted to unicode' % (value,)) ValueError: <_io.BufferedRandom name=7> could not be
> converted to unicode
Can anyone guide me what's going wrong or what I'm doing incorrectly?
任何人都可以指导我出错了什么或我做错了什么?
2 个解决方案
#1
2
As suggested by @A.Queue in(gets deleted after 29 days)
正如@ A.Queue所建议的(29天后被删除)
from google.cloud import storage
import cv2
from tempfile import TemporaryFile
client = storage.Client()
bucket = client.get_bucket('test-bucket')
image=cv2.imread('example.jpg')
with TemporaryFile() as gcs_image:
image.tofile(gcs_image)
gcs_image.seek(0)
blob = bucket.blob('example.jpg')
blob.upload_from_file(gcs_image)
The file got uploaded,but uploading a numpy ndarray
doesn't get saved as an image file on the google-cloud-storage
该文件已上传,但上传numpy ndarray并未保存为google-cloud-storage上的图像文件
PS:
numpy array
has to be convert into any image format before saving.
numpy数组必须在保存之前转换为任何图像格式。
This is fairly simple, use the tempfile
created to store the image, here's the code.
这很简单,使用创建的tempfile来存储图像,这里是代码。
with NamedTemporaryFile() as temp:
#Extract name to the temp file
iName = "".join([str(temp.name),".jpg"])
#Save image to temp file
cv2.imwrite(iName,duplicate_image)
#Storing the image temp file inside the bucket
blob = bucket.blob('ImageTest/Example1.jpg')
blob.upload_from_filename(iName,content_type='image/jpeg')
#Get the public_url of the saved image
url = blob.public_url
#2
1
You are calling blob = bucket.get_blob(gcs_image)
which makes no sense. get_blob()
is supposed to get a string argument, namely the name of the blob you want to get. A name. But you pass a file object.
你正在调用blob = bucket.get_blob(gcs_image)这没有任何意义。 get_blob()应该得到一个字符串参数,即你想要得到的blob的名称。一个名字。但是你传递了一个文件对象。
I propose this code:
我提出这个代码:
with TemporaryFile() as gcs_image:
image.tofile(gcs_image)
gcs_image.seek(0)
blob = bucket.blob('documentation-screenshots/operations/15.png')
blob.upload_from_file(gcs_image)
#1
2
As suggested by @A.Queue in(gets deleted after 29 days)
正如@ A.Queue所建议的(29天后被删除)
from google.cloud import storage
import cv2
from tempfile import TemporaryFile
client = storage.Client()
bucket = client.get_bucket('test-bucket')
image=cv2.imread('example.jpg')
with TemporaryFile() as gcs_image:
image.tofile(gcs_image)
gcs_image.seek(0)
blob = bucket.blob('example.jpg')
blob.upload_from_file(gcs_image)
The file got uploaded,but uploading a numpy ndarray
doesn't get saved as an image file on the google-cloud-storage
该文件已上传,但上传numpy ndarray并未保存为google-cloud-storage上的图像文件
PS:
numpy array
has to be convert into any image format before saving.
numpy数组必须在保存之前转换为任何图像格式。
This is fairly simple, use the tempfile
created to store the image, here's the code.
这很简单,使用创建的tempfile来存储图像,这里是代码。
with NamedTemporaryFile() as temp:
#Extract name to the temp file
iName = "".join([str(temp.name),".jpg"])
#Save image to temp file
cv2.imwrite(iName,duplicate_image)
#Storing the image temp file inside the bucket
blob = bucket.blob('ImageTest/Example1.jpg')
blob.upload_from_filename(iName,content_type='image/jpeg')
#Get the public_url of the saved image
url = blob.public_url
#2
1
You are calling blob = bucket.get_blob(gcs_image)
which makes no sense. get_blob()
is supposed to get a string argument, namely the name of the blob you want to get. A name. But you pass a file object.
你正在调用blob = bucket.get_blob(gcs_image)这没有任何意义。 get_blob()应该得到一个字符串参数,即你想要得到的blob的名称。一个名字。但是你传递了一个文件对象。
I propose this code:
我提出这个代码:
with TemporaryFile() as gcs_image:
image.tofile(gcs_image)
gcs_image.seek(0)
blob = bucket.blob('documentation-screenshots/operations/15.png')
blob.upload_from_file(gcs_image)