返回

Python generate_blob_sas不能创建正确的SAS令牌(在azcopy中复制操作失败)

发布时间:2022-04-20 09:51:06 420
# node.js

这是我在这么酷的地方问的第一个问题。

目标描述:我需要从一个Azure存储帐户复制到另一个. ...不仅是这个,这个角色现在也不适合我了。

问题:如果使用generate_blob_sas生成令牌,您将在azcopy操作中:

信息:认证失败,不正确,过期,或者没有正确的权限-> github.com/Azure/azure-storage-blob-go/azblob.newStorageError,github.com/Azure/azure-storage-blob-go@ v0.131 -0.20210914164749-2d6cd3e07548/azblob/zc_storage_error.go:42 ===== RESPONSE ERROR (ServiceCode=AuthenticationFailed) ===== Description=服务器认证请求失败。确保正确地形成了授权头的值(包括签名)。

如果在Azure portal上插入手动创建的令牌-效果良好。

为什么我在Python内部使用azcopy,因为我有这样的结构:

.
|
|└── Folder1
|    ├── file1.json
|    ├── file2.json
|    ├── file3.json
|    ├── file4.json
|    ├── file5.son
|    └── Folder2
|        ├── fileswithsomenames1.html
|        ├── fileswithsomenames2.html
|        ├── fileswithsomenames3.html
|        ├── fileswithsomenames4.html
|        ├── fileswithsomenames5.html
|        ├── fileswithsomenames6.html
|        ├── fileswithsomenames7.html
|        ├── fileswithsomenames8.html
|        ├── fileswithsomenames9.html
|        └── fileswithsomenames10.html
└──receipt.json (this file will be eachtime but with different content)

在每种情况下,文件的数量会有所不同。我需要处理来自目标blob的文件。我找不到可以理解的描述如何从store1/conteiner1/{var.name}-blob复制到var.name-store2/conteainer1/所有内容,包括许多文件夹。1 blob文件的几乎所有说明和手册。

我的代码:

code to authorise and get storage account name and string 
...
customer_name = 'abc-qa'
...
container_name_source = "container1"
blob_name_source = customer_name+"-blobfolder1/blobfolder2"

container_name_target = customer_name+"-bl"
blob_name_target = "/"

# Function to generate sas for source blob
def get_blob_sas_source(account_name_source,account_key_1_source, container_name_source, blob_name_source):
    sas_blob_source = generate_blob_sas(account_name=account_name_source, 
                                container_name=container_name_source,
                                blob_name=blob_name_source,
                                account_key=account_key_1_source,
                                permission=BlobSasPermissions(read=True),
                                expiry=datetime.utcnow() + timedelta(hours=1))
    return sas_blob_source

sas_blob_source = get_blob_sas_source(account_name_source,account_key_1_source, container_name_source, blob_name_source)
url_blob_source = 'https://'+account_name_source+'.blob.core.windows.net/'+container_name_source+'/'+blob_name_source+'?'+sas_blob_source

# Function to generate sas for target blob
def get_blob_sas_target(account_name_target,account_key_1_target, container_name_target, blob_name_target):
    sas_blob_target = generate_blob_sas(account_name=account_name_target, 
                                container_name=container_name_target,
                                blob_name=blob_name_target,
                                account_key=account_key_1_target,
                                permission=BlobSasPermissions(add=True, create=True, write=True, tag=False, delete_previous_version=True),
                                expiry=datetime.utcnow() + timedelta(hours=1))
    return sas_blob_target

sas_blob_target = get_blob_sas_target(account_name_target,account_key_1_target, container_name_target, blob_name_target)
print(sas_blob_target)
url_blob_target = 'https://'+account_name_target+'.blob.core.windows.net/'+container_name_target+'/?'+sas_blob_target




#copy seed-data folder from account_name_source/container1/customer_name+"-blobfolder1/blobfolder2" to account_name_target/customer_name+"-bl"
cmd = f"azcopy copy '{url_blob_source}' '{url_blob_target}' --recursive=True"
print(str(cmd))
results = subprocess.run(
    str(cmd), shell=True, universal_newlines=True, check=True)
print(results.stdout)

Result if gen manually 'sp=rl&st= 2012-04-14 t14:29:48 z&se = 2012-04-14 t22:29:48 z&spr =https&sv= 2012-08-04 &sr=c&sig={token}'

如果在generate_blob_sas上生成则会产生结果

“se = 2022 - 04 - 14 - t17 % 3那么% 3 a48z&sp = r&sv = 2020 - 10 - 02 - sr = b&sig ={牌}'

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像