azure-sdk-for-js icon indicating copy to clipboard operation
azure-sdk-for-js copied to clipboard

Blob Storage reference docs - request SAS 1:1 with .NET

Open diberry opened this issue 2 years ago • 6 comments

Is it possible with the existing Blob Storage SDK v12 for JS to have an equivalent of this .NET code? I can't see it in the ref docs or the samples in this SDK repo.

.NET SDK code

sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddHours(1);
sasBuilder.SetPermissions(DataLakeSasPermissions.Read | DataLakeSasPermissions.Write |DataLakeSasPermissions.List);

This is the code I have with permissions as a string in JS but I would like to use something from the SDK without a string.

JS SDK code

async function createAccountSas() {

    const sasOptions = {

        services: AccountSASServices.parse("btqf").toString(),                     // REMOVE this string to enum or something else
        resourceTypes: AccountSASResourceTypes.parse("sco").toString(), // REMOVE this string to enum or something else
        permissions: AccountSASPermissions.parse("rwdlacupi"),               // REMOVE this string to enum or something else
        protocol: SASProtocol.Https,
        startsOn: new Date(),
        expiresOn: new Date(new Date().valueOf() + (10 * 60 * 1000)),   // 10 minutes
    };

    const sasToken = generateAccountSASQueryParameters(
        sasOptions,
        sharedKeyCredential 
    ).toString();

    console.log(`sasToken = '${sasToken}'\n`);

    // prepend sasToken with `?`
    return (sasToken[0] === '?') ? sasToken : `?${sasToken}`;
}

diberry avatar Aug 09 '22 23:08 diberry

I'm not a storage expert, but it looks like the generateAccountSasUrl method on BlobServiceClient could make this a little simpler?

Also you should be able to do something like AccountSASPermissions.from({read: true, list: true}) to make the permissions a bit more legible.

xirzec avatar Aug 10 '22 16:08 xirzec

@xirzec Can you tag an engineer that might be closer to SAS token generation on JS?

diberry avatar Aug 10 '22 23:08 diberry

I think @EmmaZhu is the best point of contact for this.

xirzec avatar Aug 11 '22 18:08 xirzec

Hi @diberry ,

The permission in JS is defined as a class instead of an enum type, parsing from a string to create a permission object is recommended.

Thanks Emma

EmmaZhu avatar Aug 12 '22 01:08 EmmaZhu

@EmmaZhu Are you saying my sample code at the top of this issus is the recommended? If not, can you please provide an example of "a string".

diberry avatar Aug 12 '22 14:08 diberry

@diberry Yes, we'd recommend to input the permission with code like: permissions: AccountSASPermissions.parse("rwdlacupi")

EmmaZhu avatar Aug 15 '22 09:08 EmmaZhu

@diberry , anymore questions, please feel free to active the issue.

EmmaZhu avatar Aug 26 '22 03:08 EmmaZhu