如何使用任务链写入云存储,blobstore文件API做得很好

2024-06-16 13:13:37 发布

您现在位置:Python中文网/ 问答频道 /正文

使用blobstore文件API,我可以编写非常大的blobfile:

  • 创建blobfile
  • 从数据存储读取数据并将其写入(附加)到blobstore文件
  • 将数据存储页游标和blobstore文件传递给(下一个)任务
  • ……为同样的目的使用其他任务
  • 最后完成blobstore文件

现在有了GAE GCS客户端,我无法追加和完成。如何在不编写的情况下将非常大的文件写入GCS。撰写不是GCS客户端的一部分。文件API仍然可以正常工作,但是已经被弃用了。在

下面是使用任务链的blobstore解决方案:

class BlobData(webapp2.RequestHandler):

    def post(self):

        page = int(self.request.get('page', default_value='0'))
        data = Data.get_data(.....)

        blob_file = self.request.get('blobfile', default_value='none')
        if blob_file == 'none':
            file_name = files.blobstore.create(mime_type='text/...',
                                               _blobinfo_uploaded_filename='data....txt')
        else:
            data.with_cursor(self.request.get('cursor'))

        count = 0  # page lines counter
        with files.open(blob_file, 'a') as f:
            for each in data.fetch(page):
                f.write(each)
                count += 1

        if count >= page:
            cursor = data.cursor()
            taskqueue.add(url='/blobdata', queue_name='blobdata', countdown=10, method='POST',
                          params={'page': page, 'cursor': cursor, 'blobfile': blob_file},
                          headers={'X-AppEngine-FailFast': 'True'})
        else:  # no data left
            files.finalize(blob_file)

Tags: 文件selfapidatagetrequestcountpage
1条回答
网友
1楼 · 发布于 2024-06-16 13:13:37

在Java客户机中,我们可以序列化读取通道(相当于Python客户机中的缓冲区),并将其传递给另一个任务,以继续在同一个文件中进行写入。有关详细信息,请参见the Java doc

A readable byte channel for reading data to Google Cloud Storage. Implementations of this class may buffer data internally to reduce remote calls.

This class is Serializable, which allows for reading part of a file, serializing the GcsInputChannel deserializing it, and continuing to read from the same file from the same position.

我不知道Python GCS客户机返回的缓冲区是否可以序列化,我在doc中没有找到任何信息,但它可能值得检查。在

如果这不可能,那就用构图。不要担心GCS客户端中不提供组合,您可以始终使用appengine中的标准云存储API库。API文档在Python中有一个compose示例here。看起来像这样:

composite_object_resource = {
        'contentType': 'text/plain',  # required
        'contentLanguage': 'en',
        'metadata': {'my-key': 'my-value'},
}
compose_req_body = {
        'sourceObjects': [
                {'name': source_object_name_1,
                 'objectPreconditions': {'ifGenerationMatch': source_generation_1}},
                {'name': source_object_name_2,
                 'objectPreconditions': {'ifGenerationMatch': source_generation_2}}],
        'destination': composite_object_resource
}
req = client.objects().compose(
        destinationBucket=bucket_name,
        destinationObject=composite_object_name,
        body=compose_req_body)
resp = req.execute()
print json.dumps(resp, indent=2)

相关问题 更多 >