Mayank Patel
Mayank Patel

Reputation: 376

Get specific metadata only from List of Blobs in Azure Storage [Azure-Blob][REST]

I am successfully able to get a List of Blobs in a Container through REST calls in Azure Blob through this code.

const request = require("request");  
require("dotenv").config();

const account = process.env.ACCOUNT_NAME || "";
const key = process.env.ACCOUNT_KEY || "";
var strTime = new Date().toUTCString();
const containerName = "demo";

const BearerToken = <BearerToken>;

const options = {
  url: `https://${account}.blob.core.windows.net/${containerName}?comp=list&restype=container`,

  headers: {
    Authorization: `Bearer ${BearerToken}`,
    "x-ms-date": strTime, //var strTime = new Date().toUTCString();
    "x-ms-version": "2019-02-02", // Stable xms vesrion
  },
};

function callback(error, response, body) {
  console.log(response.body);
}

request(options, callback);

The output of the code:

 <?xml version="1.0" encoding="utf-8"?>
<EnumerationResults ServiceEndpoint="https://<storageaccount>.blob.core.windows.net/" ContainerName="demo">
    <Blobs>
        <Blob>
            <Name>
                Mayank Photo.jpg
            </Name>
            <Properties>
                <Creation-Time>
                    Fri, 12 Mar 2021 09:09:32 GMT
                </Creation-Time>
                <Last-Modified>
                    Fri, 12 Mar 2021 09:09:32 GMT
                </Last-Modified>
                <Etag>
                    0x8D8E5368CBE80AB
                </Etag>
                <Content-Length>
                    16685
                </Content-Length>
                <Content-Type>
                    image/jpeg
                </Content-Type>
                <Content-Encoding />
                <Content-Language />
                <Content-CRC64 />
                <Content-MD5>
                    AIoyEnG9amzFlWZ7t1YlCw==
                </Content-MD5>
                <Cache-Control />
                <Content-Disposition />
                <BlobType>
                    BlockBlob
                </BlobType>
                <AccessTier>
                    Hot
                </AccessTier>
                <AccessTierInferred>
                    true
                </AccessTierInferred>
                <LeaseStatus>
                    unlocked
                </LeaseStatus>
                <LeaseState>
                    available
                </LeaseState>
                <ServerEncrypted>
                    true
                </ServerEncrypted>
            </Properties>
        </Blob>
        <Blob>
            <Name>
                MayankPhoto.jpg
            </Name>
            <Properties>
                <Creation-Time>
                    Fri, 12 Mar 2021 09:10:28 GMT
                </Creation-Time>
                <Last-Modified>
                    Fri, 12 Mar 2021 09:10:28 GMT
                </Last-Modified>
                <Etag>
                    0x8D8E536AE20F3A1
                </Etag>
                <Content-Length>
                    16685
                </Content-Length>
                <Content-Type>
                    image/jpeg
                </Content-Type>
                <Content-Encoding />
                <Content-Language />
                <Content-CRC64 />
                <Content-MD5>
                    AIoyEnG9amzFlWZ7t1YlCw==
                </Content-MD5>
                <Cache-Control />
                <Content-Disposition />
                <BlobType>
                    BlockBlob
                </BlobType>
                <AccessTier>
                    Hot
                </AccessTier>
                <AccessTierInferred>
                    true
                </AccessTierInferred>
                <LeaseStatus>
                    unlocked
                </LeaseStatus>
                <LeaseState>
                    available
                </LeaseState>
                <ServerEncrypted>
                    true
                </ServerEncrypted>
            </Properties>
        </Blob>
    </Blobs>
    <NextMarker />
</EnumerationResults>

As you can see there are many other fields/metadata returned apart from the blob name is there a way to get only some specific metadata field in output eg: filename, DateCreated, ContentLength. Because sometimes I may encounter a long list of files in that case I need a short and fast response.

I think it might have something to with the metadata URL parameter in the include specified here but I don't know how to modify my URL accordingly.

Upvotes: 0

Views: 1076

Answers (2)

Gaurav Mantri
Gaurav Mantri

Reputation: 136366

The properties you listed are the system properties of a blob and they will be returned by default. There's no way to filter those properties and ask Blob Storage Service to return a few of those properties.

A blob can have additional properties which are not returned by default. One of them is user-defined metadata. To get user-defined metadata, you would need to add include=metadata in your request. Other such property is copy properties (i.e. information if a blob is created as a result of a copy operation). To see the copy information, you would need to add include=copy in your request.

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222722

It is mentioned in the documentation, you just need to have it as

https://myaccount.blob.core.windows.net/mycontainer?restype=container&comp=list&include=snapshots&include=metadata  

Upvotes: 1

Related Questions