Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(MFC) Azure Storage: List BlobsAzure Storage Blob Service REST API: Sample code to fetch the list of blobs in the specified container.
#include <CkRest.h> #include <CkAuthAzureStorage.h> #include <CkXml.h> #include <CkDateTime.h> #include <CkDtObj.h> void ChilkatSample(void) { CkString strOut; // Azure Blob Service Example: List the blobs in a specified container. // See also: https://msdn.microsoft.com/en-us/library/azure/dd135734.aspx // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. CkRest rest; // Connect to the Azure Storage Blob Service bool bTls = true; int port = 443; bool bAutoReconnect = true; // In this example, the storage account name is "chilkat". bool success = rest.Connect("chilkat.blob.core.windows.net",port,bTls,bAutoReconnect); if (success != true) { strOut.append(rest.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Provide Azure Cloud credentials for the REST call. CkAuthAzureStorage azAuth; azAuth.put_AccessKey("AZURE_ACCESS_KEY"); // The account name used here should match the 1st part of the domain passed in the call to Connect (above). azAuth.put_Account("chilkat"); azAuth.put_Scheme("SharedKey"); azAuth.put_Service("Blob"); // This causes the "x-ms-version: 2020-08-04" header to be automatically added. azAuth.put_XMsVersion("2020-08-04"); success = rest.SetAuthAzureStorage(azAuth); // Note: The application does not need to explicitly set the following // headers: x-ms-date, Authorization. These headers // are automatically set by Chilkat. // The expected success response is a 200 response status code with an XML response body. // In this example, we are listing the blobs in the container named "mycontainer". const char *responseStr = rest.fullRequestNoBody("GET","/mycontainer?restype=container&comp=list"); if (rest.get_LastMethodSuccess() != true) { strOut.append(rest.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // When successful, the Azure Storage service will respond with a 200 response status code, // with an XML response body. if (rest.get_ResponseStatusCode() != 200) { // Examine the request/response to see what happened. strOut.append("response status code = "); strOut.appendInt(rest.get_ResponseStatusCode()); strOut.append("\r\n"); strOut.append("response status text = "); strOut.append(rest.responseStatusText()); strOut.append("\r\n"); strOut.append("response header: "); strOut.append(rest.responseHeader()); strOut.append("\r\n"); strOut.append("response body (if any): "); strOut.append(responseStr); strOut.append("\r\n"); strOut.append("---"); strOut.append("\r\n"); strOut.append("LastRequestStartLine: "); strOut.append(rest.lastRequestStartLine()); strOut.append("\r\n"); strOut.append("LastRequestHeader: "); strOut.append(rest.lastRequestHeader()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Load the XML response for parsing. // An example of the response XML is shown below. CkXml xml; success = xml.LoadXml(responseStr); strOut.append(xml.getXml()); strOut.append("\r\n"); // Let's iterate over the blobs... CkDateTime lastMod; int numBlobs = xml.NumChildrenAt("Blobs"); int i = 0; while (i < numBlobs) { strOut.append("---- Blob "); strOut.appendInt(i); strOut.append(" ----"); strOut.append("\r\n"); xml.put_I(i); strOut.append("Name: "); strOut.append(xml.getChildContent("Blobs|Blob[i]|Name")); strOut.append("\r\n"); strOut.append("Content-Length: "); strOut.append(xml.getChildContent("Blobs|Blob[i]|Properties|Content-Length")); strOut.append("\r\n"); strOut.append("Content-Type: "); strOut.append(xml.getChildContent("Blobs|Blob[i]|Properties|Content-Type")); strOut.append("\r\n"); strOut.append("Content-MD5: "); strOut.append(xml.getChildContent("Blobs|Blob[i]|Properties|Content-MD5")); strOut.append("\r\n"); strOut.append("BlobType: "); strOut.append(xml.getChildContent("Blobs|Blob[i]|Properties|BlobType")); strOut.append("\r\n"); strOut.append("LeaseStatus: "); strOut.append(xml.getChildContent("Blobs|Blob[i]|Properties|LeaseStatus")); strOut.append("\r\n"); lastMod.SetFromRfc822(xml.getChildContent("Blobs|Blob[i]|Properties|Last-Modified")); CkDtObj *dt = lastMod.GetDtObj(true); strOut.append("Last-Modified YMD: "); strOut.appendInt(dt->get_Year()); strOut.append(","); strOut.appendInt(dt->get_Month()); strOut.append(","); strOut.appendInt(dt->get_Day()); strOut.append("\r\n"); delete dt; i = i + 1; } strOut.append("-------------------------"); strOut.append("\r\n"); // Use the ChilkatPath method to get information for a specific blob: // For example, let's say we want the Last-Modified date/time for // the blob named "helloWorld.txt" const char *lastModStr = xml.chilkatPath("/C/Name,helloWorld.txt|..|Properties|Last-Modified|*"); strOut.append("helloWorld.txt Last-Modified: "); strOut.append(lastModStr); strOut.append("\r\n"); strOut.append("Success."); strOut.append("\r\n"); // <?xml version="1.0" encoding="utf-8" ?> // <EnumerationResults ServiceEndpoint="https://chilkat.blob.core.windows.net/" ContainerName="test"> // <Blobs> // <Blob> // <Name>hamletC.xml</Name> // <Properties> // <Last-Modified>Mon, 02 May 2016 23:50:30 GMT</Last-Modified> // <Etag>0x8D372E48B6D323A</Etag> // <Content-Length>279658</Content-Length> // <Content-Type>application/octet-stream</Content-Type> // <Content-Encoding /> // <Content-Language /> // <Content-MD5>4E6prI9haw81xadSLeEj3Q==</Content-MD5> // <Cache-Control /> // <Content-Disposition>attachment; filename="hamletC.xml"</Content-Disposition> // <BlobType>BlockBlob</BlobType> // <LeaseStatus>unlocked</LeaseStatus> // <LeaseState>available</LeaseState> // </Properties> // </Blob> // <Blob> // <Name>helloWorld.txt</Name> // <Properties> // <Last-Modified>Mon, 02 May 2016 15:21:55 GMT</Last-Modified> // <Etag>0x8D3729D7EE38299</Etag> // <Content-Length>12</Content-Length> // <Content-Type>application/octet-stream</Content-Type> // <Content-Encoding /> // <Content-Language /> // <Content-MD5>7Qdih1MuhjZehB6Sv8UNjA==</Content-MD5> // <Cache-Control /> // <Content-Disposition>attachment; filename="helloWorld.txt"</Content-Disposition> // <BlobType>BlockBlob</BlobType> // <LeaseStatus>unlocked</LeaseStatus> // <LeaseState>available</LeaseState> // </Properties> // </Blob> // <Blob> // <Name>thisIsATest.txt</Name> // <Properties> // <Last-Modified>Tue, 03 May 2016 00:22:13 GMT</Last-Modified> // <Etag>0x8D372E8F9B2A62E</Etag> // <Content-Length>1600</Content-Length> // <Content-Type>application/octet-stream</Content-Type> // <Content-Encoding /> // <Content-Language /> // <Content-MD5>FhgUXYfePg2ItsraPmz0Xg==</Content-MD5> // <Cache-Control /> // <Content-Disposition>attachment; filename="thisIsATest.txt"</Content-Disposition> // <BlobType>BlockBlob</BlobType> // <LeaseStatus>unlocked</LeaseStatus> // <LeaseState>available</LeaseState> // </Properties> // </Blob> // </Blobs> // <NextMarker /> // </EnumerationResults> // SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); } |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.