简体   繁体   English

从Twisted上传到GCS

[英]Uploading to GCS from Twisted

I need to put files into Google's cloud storage from a twisted application. 我需要将文件从扭曲的应用程序放入Google的云存储中。

I had been using Amazon and txAWS but now I'm using GCS I'm not sure if anything exists which will let me do this? 我曾经使用过Amazon和txAWS但现在我使用的是GCS,我不确定是否存在可以让我这样做的东西?

Is it possible to use txAWS with GCS? 可以在GCS中使用txAWS吗? It sounds like an odd question but it's possible to use boto 's S3Connection with GCS so maybe there's a way to do the same with txAWS ? 听起来像是一个奇怪的问题,但是可以在GCS中使用botoS3Connection ,所以也许有一种方法可以对txAWS做同样的txAWS

I would suggest using the Twisted Web client with the GCS JSON API . 我建议将Twisted Web客户端GCS JSON API一起使用 Here's an example of listing the contents of a bucket: 这是列出存储桶内容的示例:

import json
from twisted.internet import reactor
from twisted.internet.defer import Deferred
from twisted.internet.protocol import Protocol
from twisted.web.client import Agent
from twisted.web.error import Error
from twisted.web.http_headers import Headers

GCS_BASE_URL = 'https://www.googleapis.com/storage/v1beta1'
GCS_API_KEY = '<your-api-key>'
GCS_BUCKET = '<your-bucket>'

class ResponseAccumulate(Protocol):
    def __init__(self, finished):
        self.finished = finished
        self.fullbuffer = ''

    def dataReceived(self, bytes):
        print 'Received %d bytes.' % len(bytes)
        self.fullbuffer += bytes

    def connectionLost(self, reason):
        if isinstance(reason, Error):
            print 'Finished receiving body:', reason.getErrorMessage()
        else:
            parsed = json.loads(self.fullbuffer)
            print 'Bucket contents:'
            for item in parsed['items']:
              print ' ', item['id']
        self.finished.callback(None)

agent = Agent(reactor)

d = agent.request(
    'GET',
    '%s/b/%s/o?key=%s' % (GCS_BASE_URL, GCS_BUCKET, GCS_API_KEY),
    Headers({'User-Agent': ['Twisted Web Client Example']}),
    None)

def cbResponse(response):
    print 'Response received', response.code
    finished = Deferred()
    response.deliverBody(ResponseAccumulate(finished))
    return finished
d.addCallback(cbResponse)

def cbShutdown(ignored):
    reactor.stop()
d.addBoth(cbShutdown)

reactor.run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM