从SAS URL上载到azure blob存储会返回404状态

时间:2014-08-06 09:53:32

标签: ios azure azure-storage-blobs azure-mobile-services

我正在使用Azure移动服务API端点将私有共享访问签名URL返回到我的azure存储容器,如下所示:

var blobService = azure.createBlobService(accountName, key, host);

blobService.createContainerIfNotExists(containerName, function(err) {

    if (err) {
        cb(err, null);
        return;
    }

    // Generate a 5 minute access
    var expiryDate = minutesFromNow(5);
    var sharedAccessPolicy = {
        AccessPolicy: {
            Permissions: azure.Constants.BlobConstants.SharedAccessPermissions.WRITE,
            Expiry: expiryDate
        }
    };

    // Generate the URL with read access token
    var sasURL = blobService.generateSharedAccessSignature(containerName, blobName, sharedAccessPolicy);

    var urlForDownloading = sasURL.baseUrl + sasURL.path + '?' + qs.stringify(sasURL.queryString);

    cb(null, urlForDownloading);
});

function minutesFromNow(minutes) {
    var date = new Date();
    date.setMinutes(date.getMinutes() + minutes);
    return date;
};

然后我将此URL返回到我的iOS客户端以上传文件并将其处理为:

client.invokeAPI("document/\(document.idValue).\(document.fileExtension)",
                    body: nil,
                    HTTPMethod: "PUT",
                    parameters: nil,
                    headers: nil) { result, response, error in
                        if let dictResult = result as? NSDictionary {

                            // Get the SAS URL to write directly to the blob storage
                            if let location = dictResult["location"] as? String {
                                let url = NSURL(string: location)

                                let request = NSURLRequest(URL: url)
                                let uploadTask = session.uploadTaskWithRequest(request, fromFile: localFile) { data, response, error in
                                    if completionBlock != nil {
                                        let success = (error == nil && httpResponse.statusCode == 200)
                                        completionBlock!(success)
                                     }
                                 }
                             }
                         }
} 

uploadTask.resume()

iOS客户端获得404响应,消息为

<?xml version="1.0" encoding="utf-8"?><Error><Code>ResourceNotFound</Code><Message>The specified resource does not exist.

容器确实存在于存储帐户中,并且使用访问密钥从容器中获取blob的请求是成功的。这个新的blob不会存在,因为它是一个新的上传,但为什么我得到一个404的容器写请求?

1 个答案:

答案 0 :(得分:1)

找到解决方案......

let request = NSURLRequest(URL: url)生成一个GET请求,甚至将其传递给uploadTaskWithRequest会保留此请求类型,而我认为此调用会将其更改为表示上传的PUT或POST请求。

将iOS请求定义为

let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "PUT"

成功了。返回的响应是201创建的。