在Azure中向现有的数据块添加新文本数据

huangapple go评论83阅读模式
英文:

Adding new text data to an existing blob in Azure

问题

I have a blob with data like this

2324
2321
2132

如何在这个 blob 中添加新的值?如果我添加 '2200',它将变成

2324
2321
2132
2200

我尝试过使用 append.block(),但会出现错误

> Exception: ResourceExistsError: 此操作对于该 blob 类型无效。
> RequestId: 16a8f0f9-001e-
> Time: 2023-02-24T05:05:16.1581160Z
> ErrorCode: InvalidBlobType

blob_client = container_client.get_blob_client("LIST.txt")
blob_client.append_block('5231\n')
stuff = blob_client.download_blob().readall()
ans = stuff.decode('utf-8')
ans_list = ans.split('\r\n')
# print(ans_list)
for an in ans_list:
    if an == '5231':
        print("Num Exists")

(Note: The code portion is included as requested, but the code itself is not translated.)

英文:

I have a blob with data like this

2324
2321
2132

How do I add a new value in this blob? So if I add '2200', it becomes

2324
2321
2132
2200

I have tried append.block() but that gives the error

> Exception: ResourceExistsError: The blob type is invalid for this operation.
> RequestId:16a8f0f9-001e-
> Time:2023-02-24T05:05:16.1581160Z
> ErrorCode:InvalidBlobType

    blob_client = container_client.get_blob_client("LIST.txt")
    blob_client.append_block('5231\n')
    stuff = blob_client.download_blob().readall()
    ans = stuff.decode('utf-8')
    ans_list = ans.split('\r\n')
    # print(ans_list)
    for an in ans_list:
        if an == '5231':
            print("Num Exists")

答案1

得分: 1

以下是要翻译的内容:

  1. 使用追加块:追加块是一种特殊类型的块,只能将数据追加到其中。如果您的用例始终是向块添加数据,那么您可以使用追加块而不是块块。但是,为此,您需要删除现有的块并重新创建它作为追加块。您可以在此处阅读更多关于追加块的信息:https://learn.microsoft.com/en-us/rest/api/storageservices/understanding-block-blobs--append-blobs--and-page-blobs#about-append-blobs。
  2. 下载和上传:考虑到您的块是块块,向该块添加数据的一种方法是下载块,使用修改后的数据更新其内容,然后再次上传它。这种方法对较小的块可能有效,但对于较大的块可能不是一种高效的方法。
  3. 使用块块特定功能:向块块添加数据的另一种方法是利用块块的特定功能。基本上,块块由块组成。您需要做的是下载现有块块的块列表,将新数据上传为新块,然后提交新的块列表。您可以在这里阅读更多关于此的信息:Put BlockPut Block ListGet Block List
英文:

There are 3 ways by which you can add data to an existing blob:

  1. Use append blobs: Append blobs are special kind of blobs where you can only append data to it. If your use case is to always add data to a blob, then you can use it instead of a block blob. However for this you would need to delete your existing blob and recreate it as append blob. You can read more about append blobs here: https://learn.microsoft.com/en-us/rest/api/storageservices/understanding-block-blobs--append-blobs--and-page-blobs#about-append-blobs.
  2. Download and upload: Considering your blob is a block blob, one way of adding data to that blob is to download the blob, update its contents with the modified data and then upload it again. This approach may work for smaller blobs but may not be an efficient approach for large blobs.
  3. Use block blob specific features: Another way of adding data to a block blob is to make use a block blob specific features. Essentially a block blob consists of blocks. What you have to do is download the block list of an existing blob, upload the new data as a new block and then commit the new block list. You can read more about it here: Put Block, Put Block List and Get Block List.

答案2

得分: 1

>将新的文本数据添加到Azure中的现有blob

我已经按照Document1SO-thread中的示例进行了操作:

我有以下文件:

> 2324
2321
2132

在Azure中向现有的数据块添加新文本数据

然后将其上传到存储账户:

在Azure中向现有的数据块添加新文本数据

然后执行以下代码:

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
constring = "DefaultEndpointsProtocol=pointSuffix=core.windows.net"
conname = "容器的名称"
blobname = "list.txt"

blob_service_client = BlobServiceClient.from_connection_string(constring)

container_client = blob_service_client.get_container_client(conname)

blob_client = container_client.get_blob_client(blobname)

existing_text = blob_client.download_blob().content_as_text()

add_text = "\n2200"
new_text = existing_text + add_text

blob_client.upload_blob(new_text, overwrite=True)

在Azure中向现有的数据块添加新文本数据

输出:

2200已添加如下所示:

在Azure中向现有的数据块添加新文本数据

英文:

>Adding new text data to an existing blob in Azure

I have followed Document1 and SO-thread:

I have taken a file as below:

> 2324
2321
2132

在Azure中向现有的数据块添加新文本数据

Then Uploaded it to storage account:

在Azure中向现有的数据块添加新文本数据

Then executed the below code :

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
constring = "DefaultEndpointsProtocol=pointSuffix=core.windows.net"
conname = "name of the conatiner"
blobname = "list.txt"

blob_service_client = BlobServiceClient.from_connection_string(constring)

Containerclient = blob_service_client.get_container_client(conname )

Blob_client = Containerclient.get_blob_client(blobname )

existingtext = Blob_client.download_blob().content_as_text()

addtext = "\n2200"
newtext = existingtext + addtext

Blob_client.upload_blob(newtext, overwrite=True)

在Azure中向现有的数据块添加新文本数据

Output:

2200 got added as below:

在Azure中向现有的数据块添加新文本数据

huangapple
  • 本文由 发表于 2023年2月24日 13:15:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定