如何让 GCP 函数生成的图像在 GC 存储中被识别为有效?

How to get GCP Function generated image to be recognized as valid in GC Storage?

我正在尝试在 google 云平台的功能中使用 python、运行 创建图像缩略图创建功能。图像作为 base64 字符串发送到云函数,使用 Python 的 Pillow 包进行处理和缩小。然后将其作为图像上传,从 Pillow Image 对象到 BytesIO 对象,然后保存到 google 云存储。这一切都成功完成了。

这里的问题很奇怪:Google 在手动创建访问令牌之前,云存储无法识别图像。否则,图像将陷入无限循环,永远不会加载,也永远无法使用。

我查看了this SO post,它和我的问题非常相似(这里的图片正好显示了我的问题:上传的文件无法正确加载),但它在两个重要类别上有所不同:1)他们直接操作图像数组,而我的代码从未接触过它,并且 2) 他们在 Node.js 中工作,其中 Firebase SDK 不同于 Python.

生成图片的代码如下:

def thumbnailCreator(request):
    # Setting up the resourcse we are going to use
    storage_client = storage.Client()
    stor_bucket = storage_client.bucket(BUCKET_LINK)

    # Retriving the Data
    sent_data = request.get_json()['data']
    name = sent_data['name']
    userID = sent_data['userID']

    # Process to go between base64 string to bytes, to a file object 
    imageString = stor_bucket.blob(PATH_TO_FULL_SIZE_IMAGE).download_as_string()
    
    imageFile = BytesIO(imageString)
    image = Image.open(imageFile)

    # Resizing the image is the goal 
    image = image.resize(THUMBNAIL_SIZE)

    # Go between pillow Image object to a file
    imageFile = BytesIO()
    image.save(imageFile, format='PNG')
    imageBytes = imageFile.getvalue()
    image64 = base64.b64encode(imageBytes)
    imageFile.seek(0)


    # Uploading the Data 
    other_blob = stor_bucket.blob(PATH_FOR_THUMBNAIL_IMAGE)
    other_blob.upload_from_file(imageFile, content_type = 'image/png')

    return {'data': {'response': 'ok', 'status': 200}}

同样,这有效。我感觉 MIME 类型有问题。对于这种 programming/networking/image 操作,我是新手,所以我一直在寻找更好的方法来做到这一点。无论如何,感谢您提供的所有帮助。

看来这个问题的前提 - 必须手动创建访问令牌才能使图像工作 - 不准确。经过进一步测试,错误来自我正在使用的代码库的其他部分。上面的 python 脚本确实适用于图像处理。图像的访问令牌可以通过代码生成,并在客户端提供。

留下这个,以防将来有人需要在 Google 云平台中使用 Pillow/PIL 时偶然发现它。