azure.net-coreazure-blob-storagegzipstream

Why the gzipped file uploaded to the azure blob container with a custom file extension is being downloaded as .gz via the generated sas?


Here is my method on generating the SAS:

        private string GenerateSasBlob(BlobClient blobClient)
        {
            BlobSasBuilder sasBuilder = new BlobSasBuilder()
            {
                BlobContainerName = blobClient.GetParentBlobContainerClient().Name,
                BlobName = blobClient.Name,
                Resource = "b",
                StartsOn = DateTimeOffset.UtcNow,
                ExpiresOn = DateTimeOffset.UtcNow.AddMonths(1),
                Protocol = SasProtocol.Https,
                ContentType = "mygzip"
            };
            sasBuilder.SetPermissions(BlobSasPermissions.Read);

            return blobClient.GenerateSasUri(sasBuilder).ToString();
        }

I thought that by specifying the ContentType fixes it however the generated sas still downloads as .gz instead of the expected .mygzip.

Though looking into side-by-side the contents inside of the compressed file is the same but what I need is it should be .mygzip when downloaded. Any idea on how?


Solution

  • Considering you would want the blob to be downloaded with a different extension (mygzip instead of gz), essentially you would want the blob to be downloaded with a different name.

    In that case, the response header you would want to overwrite is Content-Disposition instead of Content-Type.

    Your code would be something like:

    private string GenerateSasBlob(BlobClient blobClient)
    {
        var newBlobName = blobClient.Name.Replace(".gz", ".mygzip");//Change the extension here in the name
        BlobSasBuilder sasBuilder = new BlobSasBuilder()
        {
            BlobContainerName = blobClient.GetParentBlobContainerClient().Name,
            BlobName = blobClient.Name,
            Resource = "b",
            StartsOn = DateTimeOffset.UtcNow,
            ExpiresOn = DateTimeOffset.UtcNow.AddMonths(1),
            Protocol = SasProtocol.Https,
            ContentDisposition = $"attachment; filename=\"{newBlobName}\""
        };
        sasBuilder.SetPermissions(BlobSasPermissions.Read);
    
        return blobClient.GenerateSasUri(sasBuilder).ToString();
    }
    

    Now when a user clicks on the SAS URL, the blob will be downloaded with "mygzip" extension.

    More information on Content-Disposition header can be found here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition.