使用python在本地进行google云存储测试

时间:2018-03-27 19:40:44

标签: python google-cloud-storage blobstore

尝试在本地使用blobstore和GCS测试图像裁剪器并遇到一些问题。请记住,我没有完全弄明白GCS,这里可能存在多个问题。

def resize_and_insert_image(raw_blob_key, size, crop_info=None, is_percentage=False):
    with google_blobstore.BlobReader(raw_blob_key) as f:
        blob_data = f.read()
    img = google_images.Image(image_data=blob_data)

    if crop_info:
        # need crop coordinates as floats from 0.0 to 1.0
        width = img.width
        height = img.height
        # need crop coordinates as floats from 0.0 to 1.0
        if is_percentage:
            left_x = crop_info['x'] / 100.0
            top_y = crop_info['y'] / 100.0
            right_x = (crop_info['x'] + crop_info['width']) / 100.0
            bottom_y = (crop_info['y'] + crop_info['height']) / 100.0
        else:
            left_x = clamp(crop_info['x'] / width)
            top_y = clamp(crop_info['y'] / height)
            right_x = clamp((crop_info['x'] + crop_info['width']) / width)
            bottom_y = clamp((crop_info['y'] + crop_info['height']) / height)
        img.crop(left_x, top_y, right_x, bottom_y)

    img.resize(width=size[0], height=size[1])
    thumbnail = None
    try:
        thumbnail = img.execute_transforms(output_encoding=google_images.PNG)
    except google_images.TransformationError:
        pass
    # Retry two more times if fails on transform
    if thumbnail is None:
        try:
            thumbnail = img.execute_transforms(output_encoding=google_images.PNG)
        except google_images.TransformationError:
            pass
    if thumbnail is None:
        # This time just fail if it raises
        thumbnail = img.execute_transforms(output_encoding=google_images.PNG)

    filename = '%s-%s' % (raw_blob_key, lutil.server_now())
    mime = 'image/png'
    filepath = '/%s/%s' % (settings.GCS_DEFAULT_BUCKET, filename)
    with gcs.open(filepath, 'w', content_type=mime) as f:
        f.write(thumbnail)
    blob_key = str(google_blobstore.create_gs_key('/gs%s' % filepath))
    bk_to_cs = BlobKeyToCloudStorageManager.create(blob_key, settings.GCS_DEFAULT_BUCKET, filename, mime)
    BlobKeyToCloudStorageManager.save(bk_to_cs)

    return blob_key

它在下一行失败

with gcs.open(filepath, 'w', content_type=mime) as f: f.write(thumbnail)

给我一​​个Unable to fetch URL: {URL_here} Error: [Errno 61] Connection refused)

任何人都可以给我一些关于如何在本地测试这个问题的见解,也许我可能会遇到其他问题吗?

0 个答案:

没有答案