Azure / azure-storage-android

Microsoft Azure Storage Library for Android
Apache License 2.0
81 stars 47 forks source link

CloudBlockBlob generated a wrong SAS token #73

Open Sleepingbug opened 5 years ago

Sleepingbug commented 5 years ago

first of all, I can upload files to Azure Blob Storage Service. I'm try to generate SAS URL but failed.

static String generateBlobUrl( CloudBlockBlob blob ){

    Log.v("Test",generateBlobUrl--begin--");
    try {
        SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy();

        EnumSet aSet = EnumSet.noneOf( SharedAccessAccountPermissions.class );
        aSet.add( SharedAccessAccountPermissions.READ );
        aSet.add( SharedAccessAccountPermissions.WRITE );
        aSet.add( SharedAccessAccountPermissions.LIST );
        policy.setPermissions( aSet );

        GregorianCalendar calendar = new GregorianCalendar( TimeZone.getTimeZone("UTC"));
        calendar.setTime(new Date());
        policy.setSharedAccessStartTime(calendar.getTime());
        calendar.add( Calendar.HOUR, 24);
        policy.setSharedAccessExpiryTime(calendar.getTime());

        String sasToken = blob.generateSharedAccessSignature(policy, null );
        String host = "https://mystorage.blob.core.windows.net/"+ blob.getContainer().getName() +"/"+ blob.getName();
        String url = host +"?"+ sasToken;
        Log.v("Test",generateBlobUrl========sasToken="+ sasToken );
        Log.v("Test",generateBlobUrl========host="+ host );
        Log.v("Test",generateBlobUrl========url="+ url );
        return url;
    }
    catch ( Exception e){
        e.printStackTrace();
        Log.v("Test",generateBlobUrl--error=" + e.getLocalizedMessage() );
    }

    return null;
}

1, sasToken is wrong. log: sasToken=sig=GMqjezkLl1MSAqTyybfweB0hKvD3B5qhK9Q2ihhJefA%3D&st=2018-11-08T09%3A10%3A14Z&se=2018-11-09T09%3A10%3A14Z&sv=2017-04-17&sr=b 1) datetime format is wrong. 2) there is no "spr=https" in this sasToken

2, url is wrong too. log: url=https://mystorage.blob.core.windows.net/d89ad74fbfd74ea5969da0f5588e387b/AIRecord/20181108170953_audio.wav?sig=GMqjezkLl1MSAqTyybfweB0hKvD3B5qhK9Q2ihhJefA%3D&st=2018-11-08T09%3A10%3A14Z&se=2018-11-09T09%3A10%3A14Z&sv=2017-04-17&sr=b

when i try download from this url, i see this error:

AuthenticationFailed Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:d60e6e38-801e-00d3-3047-778133000000 Time:2018-11-08T09:45:58.9178592Z sp is mandatory. Cannot be empty