将blob的所有快照检索到单个文件中 [英] Retrieving all snapshots of a blob into a single file

查看:63
本文介绍了将blob的所有快照检索到单个文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在尝试将blob的所有快照(包括blob当前数据)检索到单个文件。我尝试了以下程序,但它无法成功并将数据存入文件。在检索数据时,我在函数
中指定了快照详细信息,请调用get_blob_to_text:


        #将blob及其快照中的内容转换为单个文件 

        tstblob = self._get_resource_reference(" cpyblob")

        self.service.put_block(container_name,tstblob,b" Hello",'1')

        block_list = [BlobBlock(id =" 1")]

        self.service.put_block_list(container_name,tstblob,block_list)



        self.service.snapshot_blob(container_name,tstblob)        #Snapshot1 

        

        self.service.put_block(container_name,tstblob,b" World",'1')

        block_list = [BlobBlock(id =" 1")]

        self.service.put_block_list(container_name,tstblob,block_list)



        self.service.snapshot_blob(container_name,tstblob)        #Snapshot2



        self.service.put_block(container_name,tstblob,b"我很好","1")

        block_list = [BlobBlock(id =" 1")]

        self.service.put_block_list(container_name,tstblob,block_list)



        list_blobs = self.service.list_blobs(container_name,prefix =" cpyblob",include = Include.SNAPSHOTS)

        fp = open('Out.txt','a')

       对于list_blobs中的blob:

           打印(blob.name)        

           打印(blob.snapshot)    

            b = self.service.get_blob_to_text(container_name,blob.name,blob.snapshot)

           打印(b.content)

            fp.write(b.content)



        fp.close()  

        fp = open('Out.txt','r')

        buf = fp.read()

        print(buf)

              




谢谢,


skri

解决方案

对延迟道歉! / p>

尝试下面提到的代码,如果您在此查询中需要更多帮助,请告知我们。

# -  *  - 编码:utf-8  -  *  -  
"""
创建于5月8日星期三11:41:04 2019

@author:moverm
"""
导入os,uuid,sys
来自azure.storage.blob import BlockBlobService,PublicAccess


def run_sample():
try:
#创建用于为存储帐户调用Blob服务的BlockBlockService
block_blob_service = BlockBlobService(account_name ='#####',account_key ='####')

#创建一个名为'quickstartblobs'的容器。
container_name ='quickstartblob'
block_blob_service.create_container(container_name)

#设置权限以使blob是公共的。
block_blob_service.set_container_acl(container_name,public_access = PublicAccess.Container)

#在Documents中创建一个文件来测试上传和下载。
local_path = os.path.expanduser("〜/ Documents")
local_file_name =" QuickStart1.txt"
full_path_to_file = os.path.join(local_path,local_file_name)

#将文本写入文件。
file = open(full_path_to_file,'w')
file.write(" Hello,World!")
file.close()

print( " Temp file =" + full_path_to_file)
print(" \ nnpup to Blob storage as blob" + local_file_name)

#上传创建的文件,使用local_file_name作为blob名称
block_blob_service.create_blob_from_path(container_name,local_file_name,full_path_to_file)

#列出容器中的blob
print(容器中的"\ nList blobs")
generator = block_blob_service.list_blobs(container_name)
for blob in generator:
print(" \t Blob name:" + blob.name)

#下载blob (S)。
#将'_DOWNLOADED'添加为'.txt'的前缀,这样您就可以在Documents中看到这两个文件。
full_path_to_file2 = os.path.join(local_path,str.replace(local_file_name,'。txt','_ DOWNLOADED.txt'))
print(" \\\
Downloading blob to" + full_path_to_file2)
#block_blob_service.get_blob_to_path(container_name,local_file_name,full_path_to_file2)

block_blob_service.put_block(container_name,local_file_name,b" World",'1')
block_blob_service.snapshot_blob(container_name, local_file_name)
print(" \ nnnding first snapshot")
block_blob_service.put_block(container_name,local_file_name,b" I am Feeling Lucky",'2')
block_blob_service.snapshot_blob(container_name ,local_file_name)
print(" \ nnAdding first snapshot")

#list_blobs = block_blob_service.list_blobs(container_name,prefix =" QuickStart1",include ='snapshots')
#fp = open('Out.txt ,'a')block_blob_service.list_blobs中blob的
(container_name,prefix =" QuickStart1",include ='snapshots'):
print(blob.name,blob.snapshot)
block_blob_service.get_blob_to_path (container_name,blob.name,full_path_to_file2,open_mode ='ab',snapshot = blob.snapshot,start_range = 0)

除了异常为e:
print(e)


#主要方法。
如果__name__ =='__ main__':
run_sample()


您也可以参考
SO
主题其中有类似的问题



请告知我们上述内容是否有帮助,或者您需要在此问题上获得进一步的帮助。


------------------------------------------------ ------------------------------------------


点击帮助您的帖子上的"标记为答案",这对其他
社区成员有益。


Hi All,

I am trying to retrieve all snapshots of a blob including the blob current data to a single file. I tried the following program but it couldn't succeeded and get the data to a file. While retrieving the data i am specifying the snapshot detail in the function call get_blob_to_text:

        # To get contents from blob and its snapshots to a single file 
        tstblob = self._get_resource_reference("cpyblob")
        self.service.put_block(container_name, tstblob, b"Hello", '1')
        block_list = [BlobBlock(id = "1")]
        self.service.put_block_list(container_name, tstblob, block_list)

        self.service.snapshot_blob(container_name, tstblob)        # Snapshot1 
        
        self.service.put_block(container_name, tstblob, b"World", '1')
        block_list = [BlobBlock(id = "1")]
        self.service.put_block_list(container_name, tstblob, block_list)

        self.service.snapshot_blob(container_name, tstblob)        # Snapshot2

        self.service.put_block(container_name, tstblob, b"I am fine", '1')
        block_list = [BlobBlock(id = "1")]
        self.service.put_block_list(container_name, tstblob, block_list)

        list_blobs = self.service.list_blobs(container_name, prefix="cpyblob", include=Include.SNAPSHOTS)
        fp = open('Out.txt', 'a')
        for blob in list_blobs:
            print(blob.name)        
            print(blob.snapshot)    
            b = self.service.get_blob_to_text(container_name, blob.name, blob.snapshot)
            print(b.content)
            fp.write(b.content)

        fp.close()  
        fp = open('Out.txt', 'r')
        buf = fp.read()
        print(buf)
              

Thanks,

skri

解决方案

Apologies for the delay!

Try the code mentioned below and let me know if you need any more assistance on this query 

# -*- coding: utf-8 -*-
"""
Created on Wed May  8 11:41:04 2019

@author: moverm
"""
import os, uuid, sys
from azure.storage.blob import BlockBlobService, PublicAccess


def run_sample():
    try:
        # Create the BlockBlockService that is used to call the Blob service for the storage account
        block_blob_service = BlockBlobService(account_name='#####', account_key='####')

        # Create a container called 'quickstartblobs'.
        container_name ='quickstartblob'
        block_blob_service.create_container(container_name)

        # Set the permission so the blobs are public.
        block_blob_service.set_container_acl(container_name, public_access=PublicAccess.Container)

        # Create a file in Documents to test the upload and download.
        local_path=os.path.expanduser("~/Documents")
        local_file_name ="QuickStart1.txt"
        full_path_to_file =os.path.join(local_path, local_file_name)

        # Write text to the file.
        file = open(full_path_to_file,  'w')
        file.write("Hello, World!")
        file.close()

        print("Temp file = " + full_path_to_file)
        print("\nUploading to Blob storage as blob" + local_file_name)

        # Upload the created file, use local_file_name for the blob name
        block_blob_service.create_blob_from_path(container_name, local_file_name, full_path_to_file)

        # List the blobs in the container
        print("\nList blobs in the container")
        generator = block_blob_service.list_blobs(container_name)
        for blob in generator:
            print("\t Blob name: " + blob.name)

        # Download the blob(s).
        # Add '_DOWNLOADED' as prefix to '.txt' so you can see both files in Documents.
        full_path_to_file2 = os.path.join(local_path, str.replace(local_file_name ,'.txt', '_DOWNLOADED.txt'))
        print("\nDownloading blob to " + full_path_to_file2)
        #block_blob_service.get_blob_to_path(container_name, local_file_name, full_path_to_file2)

        block_blob_service.put_block(container_name, local_file_name, b"World", '1')
        block_blob_service.snapshot_blob(container_name,local_file_name)
        print("\nAdding first snapshot")     
        block_blob_service.put_block(container_name, local_file_name, b"I am Feeling Lucky", '2')
        block_blob_service.snapshot_blob(container_name,local_file_name)
        print("\nAdding first snapshot")  
        
        #list_blobs = block_blob_service.list_blobs(container_name, prefix="QuickStart1", include='snapshots')
        #fp = open('Out.txt', 'a')
        for blob in block_blob_service.list_blobs(container_name,prefix="QuickStart1", include='snapshots'):    
            print (blob.name, blob.snapshot)
            block_blob_service.get_blob_to_path(container_name,blob.name, full_path_to_file2,open_mode='ab',snapshot=blob.snapshot,start_range=0)
            
    except Exception as e:
        print(e)


# Main method.
if __name__ == '__main__':
    run_sample()

You may also refer to SO thread which has the similar issue

Kindly let us know if the above helps or you need further assistance on this issue.

------------------------------------------------------------------------------------------

Do click on "Mark as Answer" on the post that helps you, this can be beneficial to other community members.


这篇关于将blob的所有快照检索到单个文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆