【问题标题】:How to Upload Croppie Photo to Google Cloud Storage如何将 Croppie 照片上传到谷歌云存储
【发布时间】:2018-10-02 20:43:29
【问题描述】:

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

【问题讨论】:

    标签: python google-app-engine google-cloud-storage croppie


    【解决方案1】:

    我的解决方案是使用 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"
            ]
        ]);
    

    【讨论】:

      【解决方案2】:

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

      详情请查看this post

      【讨论】:

      • 我觉得自己像个彻头彻尾的白痴……我正在使用以下代码来创建我的文件,但它在 open 处中断。 GAE 不支持 open 吗? img_data = b'iVB.... fh = open("imageToSave.png", "wb") fh.write(img_data.decode('base64')) fh.close()
      • 不,GAE 不支持打开本地文件的任何内容。您必须使用 FileStream 而不是实际文件才能使其工作。
      【解决方案3】:

      您可以在 App Engine 中使用 images 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()
      

      【讨论】:

      • 这已接近尾声,但仍不能 100% 工作。您介意在第一行“my_image_file.read()”和我在原始问题中提到的 4 个选项中详细介绍一下,我应该使用什么数据格式? Base64?
      • 你有图片的 URL 吗?还是它的字节?我已经用代码编辑了答案以从 url 获取
      • 嗯,它不是网址,而是imagedata = _some base64data,我不知道如何将图像数据转换为图像文件以存储在谷歌云存储中
      • 您是否尝试按照我回答中的最后几行代码将数据写入image_bytes
      猜你喜欢
      • 2021-06-09
      • 2015-03-01
      • 1970-01-01
      • 2016-04-12
      • 2022-10-30
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 2016-07-31
      相关资源
      最近更新 更多