Java 如何生成SAS令牌并使用它将文件上载到Azure Blob存储?

Java 如何生成SAS令牌并使用它将文件上载到Azure Blob存储?,java,scala,azure,azure-blob-storage,Java,Scala,Azure,Azure Blob Storage,我正在生成一个Azure SAS令牌,以便像这样将文件上载到Azure sig=XXXXXXXXXXXXXXX&st=2020-09-04T00%3A13%3A56Z&se=2020-09-04T10%3A13%3A56Z&sv=2019-02-02&spr=https&sp=cw&sr=b * upload completely sent off: 8 out of 8 bytes < HTTP/1.1 403 Server failed

我正在生成一个Azure SAS令牌,以便像这样将文件上载到Azure

sig=XXXXXXXXXXXXXXX&st=2020-09-04T00%3A13%3A56Z&se=2020-09-04T10%3A13%3A56Z&sv=2019-02-02&spr=https&sp=cw&sr=b
* upload completely sent off: 8 out of 8 bytes
< HTTP/1.1 403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
< Content-Length: 321
< Content-Type: application/xml
< Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
< x-ms-request-id: f5fd7e6b-601e-0058-681b-837a50000000
< Date: Sat, 05 Sep 2020 00:30:47 GMT
< 
<?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:f5fd7e6b-601e-0058-681b-837a50000000
* Connection #0 to host <AccountName>.blob.core.windows.net left intact
Time:2020-09-05T00:30:47.9553766Z</Message></Error>* Closing connection 0
//创建客户端
val storageConnectionString=s“DefaultEndpointsProtocol=https;AccountName=${AccountName};AccountKey=${AccountKey}”
val storageAccount=CloudStorageAccount.parse(storageConnectionString)
val client=storageAccount.createCloudBlobClient()
//获取blob客户端
val container=client.getContainerReference(containerName)
val blockBlob=container.getBlockBlobReference(路径)
//生成SAS令牌
val策略=新SharedAccessBlobPolicy()
policy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.CREATE,SharedAccessBlobPermissions.WRITE))
val now=新日期()
val calendar=calendar.getInstance()
calendar.setTime(现在)
calendar.add(calendar.DATE,-1)
val start=calendar.getTime
calendar.add(calendar.HOUR,10)
val end=calendar.getTime
policy.setSharedAccessStartTime(开始)
policy.setSharedAccessExpiryTime(结束)
val token=blockBlob.generateSharedAccessSignature(策略,空)
上面逻辑的输出是这样的

sig=XXXXXXXXXXXXXXX&st=2020-09-04T00%3A13%3A56Z&se=2020-09-04T10%3A13%3A56Z&sv=2019-02-02&spr=https&sp=cw&sr=b
* upload completely sent off: 8 out of 8 bytes
< HTTP/1.1 403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
< Content-Length: 321
< Content-Type: application/xml
< Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
< x-ms-request-id: f5fd7e6b-601e-0058-681b-837a50000000
< Date: Sat, 05 Sep 2020 00:30:47 GMT
< 
<?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:f5fd7e6b-601e-0058-681b-837a50000000
* Connection #0 to host <AccountName>.blob.core.windows.net left intact
Time:2020-09-05T00:30:47.9553766Z</Message></Error>* Closing connection 0
现在,我试图在上传文件的PUT请求中将此令牌添加到
授权
头中

$ curl -X PUT -H 'Accept: */*' -H 'Content-Type: application/json' -H 'Authorization: SharedAccessSignature sig=XXXXXXXXXXXXXXX&st=2020-09-04T00%3A13%3A56Z&se=2020-09-04T10%3A13%3A56Z&sv=2019-02-02&spr=https&sp=cw&sr=b' -d '{}' https://<AccountName>.blob.core.windows.net/<ContainerName>/test.json -v
我在
build.sbt
中的依赖项:

lazy val azureLibs = Seq(
  "com.azure" % "azure-storage-blob" % "12.7.0",
  "com.microsoft.azure" % "azure-storage" % "8.6.5"
)

如何正确生成SAS令牌并使用它将文件上载到Azure Blob存储?

我将解决方案总结如下


如果我们想使用共享访问签名(SAS令牌)来管理Azure blob,我们需要使用SAS令牌作为查询字符串。因此请求url应该类似于
https://

根据您的代码,您可以创建sas令牌。如果是这样,您需要使用脚本
curl-xput-H'Accept://*'-H'内容类型:application/json'-d'{}https://Echoing 上面的评论——你就快到了。SAS标记应该用作一个查询字符串,附加到您试图访问的blob的URL。如果您是对的,我应该注意到,正是这一部分让我错了,因为他们将其用作头,而不是query param@bachr Hi。我已将我的建议总结为一种解决办法。此外,既然它对你们很有用,你们能接受它作为一个答案吗?把令牌放进URL是非常糟糕的做法。这是微软推荐的吗?