【发布时间】:2020-10-19 20:08:54
【问题描述】:
我正在使用 SAS 令牌从 React spa 将文件上传和下载到 Azure 存储。
在本地主机上运行时,一切正常,但是当部署到 Azure 上的 Kubernetes 时,我收到以下身份验证错误。
onError RestError: <?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:e6bfca97-c01e-0030-2e29-4e7d7c000000
Time:2020-06-29T15:26:39.7164613Z</Message><AuthenticationErrorDetail>Signature did not match. String to sign used was w
2020-06-29T20:26:39Z
/blob/datalake/container/Natural_Language_Processing.pdf
负责上传的javascript代码是
// upload to Azure
const blobName = file.name;
const accountSas = resp.data.SAS;
const account = resp.data.account;
const containerName = resp.data.container;
const anonymousCredential = new AnonymousCredential();
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net?${accountSas}`,
anonymousCredential
);
// Create a container
const containerClient = blobServiceClient.getContainerClient(
containerName
);
// Create a blob
const content = file;
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.upload(
content,
Buffer.byteLength(content)
);
而用于 SAS 令牌生成的后端 Python 代码如下
if content['up_down'] == 'download':
permission = BlobSasPermissions(read=True)
else:
permission = BlobSasPermissions(write=True)
account_name = os.getenv("STORAGE_ACCOUNT_NAME")
container_name = metadata.get_container_name()
blob_name = content['filePath']
expiry = datetime.utcnow() + timedelta(hours=5)
options = {
'account_name': account_name,
'container_name': container_name,
'blob_name': blob_name,
'account_key': os.getenv("STORAGE_ACCESS_KEY"),
'permission': permission,
'expiry': expiry
}
SAS = generate_blob_sas(**options)
generate_blob_sas 是从 azure-storage-blob(版本 12.3.1)导入的。
知道如何解决这个问题吗?
【问题讨论】:
-
我注意到有一个 IP 地址 (
84.71.57.183),但我在options中没有看到。这可能是问题的原因吗? -
感谢@Gaurav Mantri-AIS 的帮助。一开始我尝试不指定客户端IP,但仍然无法正常工作。之后,我尝试在请求 sas 令牌时指定客户端 ip(不包含在上面的 Python sn-p 中),但仍然没有运气。
-
您如何访问本地的
STORAGE_ACCESS_KEY?您是否还将STORAGE_ACCESS_KEY传递给backend-service的kubernetes pod? -
感谢@Dhruv Shah 的帮助。
STORAGE_ACCESS_KEY在 Kubernetes 中作为秘密传递并作为环境变量加载。只是为了看看这是否是错误,我暂时返回STORAGE_ACCESS_KEY作为 API 响应的一部分,它匹配 -
您可以编辑您的问题并包含 generate_blod_sas 方法的代码吗?
标签: javascript python azure kubernetes azure-storage