【问题标题】:Download image data then upload to Google Cloud Storage下载图像数据,然后上传到 Google Cloud Storage
【发布时间】:2017-01-01 05:35:40
【问题描述】:

我有一个在 Google AppEngine 上运行的 Flask 网络应用。该应用程序有一个表单,我的用户将使用它来提供图像链接。我想从链接下载图像数据,然后将其上传到 Google Cloud Storage 存储桶。

到目前为止,我在 Google 的文档中发现的内容告诉我使用已安装并导入为“gcs”的“cloudstorage”客户端库。
在这里找到:https://cloud.google.com/appengine/docs/python/googlecloudstorageclient/read-write-to-cloud-storage

我认为我没有通过请求正确处理图像数据。我从 Cloud Storage 上传调用中得到了 200 个代码,但是当我在控制台中查找它时没有对象。这是我尝试检索图像然后上传的地方:

img_resp = requests.get(image_link, stream=True)
objectName = '/myBucket/testObject.jpg'

gcs_file = gcs.open(objectName,
                    'w',
                    content_type='image/jpeg')
gcs_file.write(img_resp)
gcs_file.close()

编辑: 这是我更新的代码以反映答案的建议:

image_url = urlopen(url)
content_type = image_url.headers['Content-Type']
img_bytes = image_url.read()
image_url.close()

filename = bucketName + objectName
options = {'x-goog-acl': 'public-read',
           'Cache-Control': 'private, max-age=0, no-transform'}

with gcs.open(filename,
              'w',
              content_type=content_type,
              options=options) as f:
    f.write(img_bytes)
    f.close()

但是,我仍然在 POST(创建文件)调用中​​收到 201 响应,然后在 PUT 调用中收到 200,但该对象从未出现在控制台中。

【问题讨论】:

    标签: python-2.7 google-app-engine flask python-requests google-cloud-storage


    【解决方案1】:

    试试这个:

    from google.appengine.api import images
    import urllib2
    
    image = urllib2.urlopen(image_url)
    
    img_resp = image.read()
    image.close()
    
    objectName = '/myBucket/testObject.jpg'
    options = {'x-goog-acl': 'public-read', 
             'Cache-Control': 'private, max-age=0, no-transform'}
    
    with gcs.open(objectName,
                  'w',
                  content_type='image/jpeg', 
                  options=options) as f:
    
        f.write(img_resp)
        f.close()
    

    而且,为什么要限制他们只输入一个网址。为什么不允许他们上传本地图片:

    if isinstance(image_or_url, basestring):    # should be url
        if not image_or_url.startswith('http'):
            image_or_url = ''.join([ 'http://', image_or_url])
        image = urllib2.urlopen(image_url)
        content_type =  image.headers['Content-Type']
        img_resp = image.read()
        image.close()
    else:
        img_resp = image_or_url.read()
        content_type = image_or_url.content_type
    

    如果您在开发服务器上运行,该文件将被上传到您的本地数据存储中。检查它:

    http://localhost:<your admin port number>/datastore?kind=__GsFileInfo__
    

    http://localhost:<your admin port number>/datastore?kind=__BlobInfo__
    

    【讨论】:

    • 桶中仍然没有对象出现。我从 GCS 获得 201 用于创建对象调用,然后 200 用于 PUT 调用,这不意味着它成功了吗?此外,它在 dev_appserver.py 服务器上运行
    • 我刚刚编辑为存储桶添加了一些选项,并使用with 语句进行写入。您可以检查表单输入,它是指向 jpg 图像的正确 url 吗? (最好设置content_type=content_type,并以编程方式获取content_type,就像我回答的第二部分一样。)
    • 你的myBucket == &lt;your-app-id&gt;.appspot.com 是吗?您可能希望将图像放在伪目录中,例如 /&lt;your-app-id&gt;.appspot.com/myImages/
    • 我刚刚更改了代码以反映您的编辑,并确保我的存储桶名称正确并添加了一个伪目录。仍然得到 201 然后是 200 但没有对象
    • 查看本地开发服务器的编辑。这没有很好的记录。
    猜你喜欢
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 2016-11-09
    相关资源
    最近更新 更多