1

在被croppie裁剪到GCS后,我正在尝试上传照片。gcs write(imageToUpload) 方法失败,因为 imageToUpload 当前不是文件,而是 BLOB。有谁知道使这项工作的方法?也许,python 中将 BLOB 转换为文件的一种方法?可以从 Croppie 返回的图像类型有:1) Canvas、2) HTML、3) BLOB 和 4)Base64这是文档 (Ctrl-F "Result")。任何帮助将非常感激!

4

3 回答 3

0

我的解决方案是使用 PHP,但我认为您将能够理解并将其转换为 python。

Croppie 结果方法:-

logoCropper.croppie('result', {
    type: 'base64',
    size: 'viewport'
})
.then(function (resp) {
   // sends the data to the server using an AJAX call
   // AJAX call data 
   //     data: {
   //       'imagebase64': resp
   //     }
});

PHP端点:-

$imageName = $some_name.".png";

// extracting the base64 encoded string from the request payload
$imageArray = explode(";", $request->get('imagebase64'));
$imageContents = explode(",", $imageArray[1]);

$imagebase64 = base64_decode($imageContents[1]);

// saving the image to a temp file
$tempPath = sys_get_temp_dir()."/".$imageName;
file_put_contents($tempPath, $imagebase64);

$storageClient = new StorageClient([
        'projectId' => $project_id,
        'keyFilePath' => $credentials_path,
]);

$bucket = $storageClient->bucket($bucketName);
$adapter = new GoogleStorageAdapter($storageClient, $bucket);

$filesystem = new Filesystem($adapter);

// uploading to the google bucket
$stream = fopen($tempPath, 'r+');
$filesystem->writeStream($storage_folder."/".$imageName, $stream, [
        'visibility' => AdapterInterface::VISIBILITY_PRIVATE,
        'metadata' => [
            'contentDisposition' => "attachment; filename={".$imageName."}",
            'ContentType' => "image/png"
        ]
    ]);
于 2021-09-15T10:34:07.137 回答
0

您真正需要做的只是将 base64 格式转换为本地文件,以便上传。

请查看此帖子了解详情。

于 2018-04-23T18:05:55.983 回答
0

您可以images在 App Engine 中使用 pkg。

from google.appengine.api import images
import urllib2

# if you fetch as image
image        = urllib2.urlopen(the_image_url)   
content_type =  image.headers['Content-Type'] 
image_bytes  = image.read()
image.close()

# if you fetch as bytes
image_bytes  = urllib2.urlopen(the_image_bytes_url) 
content_type = my_image_file.content_type  

filename = '/{}/user/{}_avatar'.format(DEFAULT_GCS_BUCKET_NAME), user_id) # for example

'''
    Create a GCS file with GCS client.
    Make sure you give the GAE app permission to write to your bucket:
    https://developers.google.com/appengine/docs/python/googlestorage/index#Prerequisites
'''

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(image_bytes)
    f.close()
于 2018-04-23T04:49:14.797 回答