使用谷歌应用引擎python将图像从外部链接上传到谷歌云存储

时间:2014-08-20 18:40:24

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

我正在寻找一个解决方案,了解如何使用appengine python将图片从http://example.com/image.jpg等外部网址上传到Google云端存储,

我现在正在使用

blobstore.create_upload_url('/uploadSuccess', gs_bucket_name=bucketPath)

对于想要从计算机上传图片的用户,请致电

images.get_serving_url(gsk,size=180,crop=True)

在uploadSuccess上并将其存储为个人资料图片。我尝试允许用户在使用oauth2登录后使用他们的脸书或谷歌个人资料图片。我可以访问他们的个人资料图片链接,我只想复制它以保持一致性。皮斯帮助:)

3 个答案:

答案 0 :(得分:9)

要上传外部图片,您必须获取并保存。 要获取图像,请使用this code

from google.appengine.api import urlfetch

file_name = 'image.jpg'
url = 'http://example.com/%s' % file_name
result = urlfetch.fetch(url)
if result.status_code == 200:
    doSomethingWithResult(result.content)

要保存图片,您可以使用应用引擎GCS客户端代码shown here

import cloudstorage as gcs
import mimetypes

doSomethingWithResult(content):

    gcs_file_name = '/%s/%s' % ('bucket_name', file_name)
    content_type = mimetypes.guess_type(file_name)[0]
    with gcs.open(gcs_file_name, 'w', content_type=content_type,
                  options={b'x-goog-acl': b'public-read'}) as f:
        f.write(content)

    return images.get_serving_url(blobstore.create_gs_key('/gs' + gcs_file_name))

答案 1 :(得分:0)

如果您正在寻找依靠function checkWebP(callback) { var webP = new Image(); webP.onload = webP.onerror = function () { callback(webP.height == 2); }; webP.src = 'data:image/webp;base64,UklGRjoAAABXRUJQVlA4IC4AAACyAgCdASoCAAIALmk0mk0iIiIiIgBoSygABc6WWgAA/veff/0PP8bA//LwYAAA'; }; checkWebP(function(support) { if(support) { //Do what you whant =) console.log('work webp'); }else{ //Do what you whant =) console.log('not work, use jgp/png') } })软件包进行更新的方式,我写了这两个函数:

end

然后通常调用storages并从系统内的任何地方传递import requests from storages.backends.gcloud import GoogleCloudStorage def download_file(file_url, file_name): response = requests.get(file_url) if response.status_code == 200: upload_to_gc(response.content, file_name) def upload_to_gc(content, file_name): gc_file_name = "{}/{}".format("some_container_name_here", file_name) with GoogleCloudStorage().open(name=gc_file_name, mode='w') as f: f.write(content) download_file()

url来自prefered_file_name包。

GoogleCloudStorage

Django Storages

答案 2 :(得分:0)

这是我的新解决方案(2019),仅使用When I use scanner method below I obtain the correct answer which is 12.0 8.0 5.0 4.5 5.0 6.0 Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; for(int i=0; i < a.length; i++){ a[i] = in.nextInt(); } getMedians(a); } When I modify code below to use array public static void main(String[] args) { double [] adio = {6.0, 12.0, 4.0, 5.0, 3.0, 8.0, 7.0 }; double[] a = adio.clone(); getMedians(a); } I get the 6.0 9.0 6.0 5.5 5.0 5.5 6.0 Please how do I modify this code using array to get the correct answer 12.0 8.0 5.0 4.5 5.0 6.0 库和google-cloud-storage函数(请参见here):

upload_from_string()