代码之家  ›  专栏  ›  技术社区  ›  Michael S. Scherotter

无法上载带有SAS令牌的BLOB

  •  0
  • Michael S. Scherotter  · 技术社区  · 9 年前

    我有一个使用Azure移动应用程序构建的Xamarin UWP应用程序,我正在尝试使用SAS令牌和将BLOB上传到Azure存储。Net Azure Storage client(8.4.0)。我可以通过一个简单的API节点函数为我的容器生成SAS令牌,但当我尝试使用它上载文件时,我遇到了一个错误“ 服务器无法验证请求。确保包括签名在内的授权标头的值格式正确。 "

    简易API代码:用于GetUploadToken

    var azure = require('azure-storage');
    
    function generateSasToken(container, blobName, permissions) {
        var connString = "<conn string from azure storage acct>"; // removed for StackOverflow question
        var blobService = azure.createBlobService(connString);
    
        var currentDate = new Date();
        var startDate = new Date(currentDate);
        startDate.setMinutes(currentDate.getMinutes() - 5);
    
        console.log("startDate:    " + startDate);
    
        console.log("current date: " + currentDate);
    
        var expiryDate = new Date(currentDate);
        expiryDate.setMinutes(currentDate.getMinutes() + 60);
    
        console.log("expiryDate:   " + expiryDate);    
    
        permissions = permissions || azure.BlobUtilities.SharedAccessPermissions.READ;
    
        var sharedAccessPolicy = {
            AccessPolicy: {
                Permissions: permissions,
                Start: startDate,
                Expiry: expiryDate
            }
        };
    
        var sasToken = blobService.generateSharedAccessSignature(container, blobName, sharedAccessPolicy);
    
        console.log("SAS Token: " + sasToken);
        return {
            token: sasToken,
            uri: blobService.getUrl(container, blobName, sasToken)
        };
    }
    
    module.exports = {
        "post": function (req, res, next) {
            if (req.body.Container) {
               console.log("Container: " + req.body.Container);
               console.log("BlobName:" + req.body.BlobName);
               console.log("Permissions:" + req.body.Permissions);
    
                // The following values can be used for permissions: 
                // "a" (Add), "r" (Read), "w" (Write), "d" (Delete), "l" (List)
                // Concatenate multiple permissions, such as "rwa" = Read, Write, Add
                var token = generateSasToken(req.body.Container, req.body.BlobName, req.body.Permissions);
    
                res.status(200).type('application/json').json(token);
            } else {
                res.status(400).type("text/plain").text("Specify a value for 'container");
            }
        }
    };
    

    轻松的API输出

    Container: sketches
    BlobName:DSCF3726.JPG
    Permissions:rwa
    startDate:    Thu Oct 05 2017 14:39:36 GMT+0000 (Coordinated Universal Time)
    current date: Thu Oct 05 2017 14:44:36 GMT+0000 (Coordinated Universal Time)
    expiryDate:   Thu Oct 05 2017 15:44:36 GMT+0000 (Coordinated Universal Time)
    SAS Token: st=2017-10-05T14%3A39%3A36Z&se=2017-10-05T15%3A44%3A36Z&sp=rwa&sv=2017-04-17&sr=b&sig=...
    

    上载代码

    internal async Task<string> UploadAsync(string fileName, byte[] dataArray)
    {
        var sketchFile = new SketchFile
        {
            BlobName = fileName,
            Container = "sketches",
            Permissions = "rwa"
        };
    
        var response = await CurrentClient.InvokeApiAsync<SketchFile, GetSasTokenResponse>(
            "GetUploadToken", 
            sketchFile);
    
        try
        {
            var blob = new CloudBlockBlob(new Uri(response.Uri));
    
            await blob.UploadFromByteArrayAsync(dataArray, 0, dataArray.Length);
        }
        catch (StorageException se)
        {
            System.Diagnostics.Debug.WriteLine($"Error uploading {fileName}: {se.RequestInformation.ExtendedErrorInformation.ErrorMessage}.");
    
            return null;
        }
    
        var uri = response.Uri.Substring(0, response.Uri.IndexOf('?'));
    
        return uri;
    }
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Bruce Chen    9 年前

    根据你的代码,我可以在我这边重现这个问题。我认为这可能是由于 Permissions Permissions for a blob 关于 Add 许可如下:

    允许的操作:将块添加到附加blob。

    Azure Storage Explorer 并为块blob生成sas令牌以缩小此问题。

    权限 到 rw