Sample code for 30+ languages & platforms
Swift

Azure Blob - Get Tags (Check if Blob Exists)

See more Azure Cloud Storage Examples

Gets the user-defined tags for a specified blob. This can also be used as a way to check to see if a blob exists.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let rest = CkoRest()!

    // Connect to the Azure Storage Blob Service
    var bTls: Bool = true
    var port: Int = 443
    var bAutoReconnect: Bool = true
    // In this example, the storage account name is "chilkat".
    success = rest.connect(hostname: "chilkat.blob.core.windows.net", port: port, tls: bTls, autoReconnect: bAutoReconnect)
    if success != true {
        print("\(rest.lastErrorText!)")
        return
    }

    // Provide Azure Cloud credentials for the REST call.
    let azAuth = CkoAuthAzureStorage()!
    azAuth.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.account = "chilkat"
    azAuth.scheme = "SharedKey"
    azAuth.service = "Blob"
    // This causes the "x-ms-version: 2021-08-06" header to be automatically added.
    azAuth.xMsVersion = "2021-08-06"
    success = rest.setAuthAzureStorage(authProvider: azAuth)

    // Note: The application does not need to explicitly set the following
    // headers: Content-Length, x-ms-date, Authorization.  These headers
    // are automatically set by Chilkat.

    // The Azure blob container is "test", the file is "helloWorld.txt"
    // Add "?comp=tags" to get the user-defined tags instead of the actual blob content.
    let sb = CkoStringBuilder()!
    success = rest.fullRequestNoBodySb(httpVerb: "GET", uriPath: "/test/helloWorldajdfadf.txt?comp=tags", sb: sb)
    if success == false {
        // Examine the request/response to see what happened.
        print("response status code = \(rest.responseStatusCode.intValue)")
        print("response status text = \(rest.responseStatusText!)")
        print("response header: \(rest.responseHeader!)")
        print("---")
        print("LastRequestStartLine: \(rest.lastRequestStartLine!)")
        print("LastRequestHeader: \(rest.lastRequestHeader!)")
        return
    }

    // A typical blob with no tags will return this:

    // <?xml version="1.0" encoding="utf-8"?>
    // <Tags><TagSet/></Tags>

    // If the blob does not exist, you get this:

    // <?xml version="1.0" encoding="utf-8"?>
    // <Error>
    //     <Code>BlobNotFound</Code>
    //     <Message>The specified blob does not exist.
    // 	RequestId:fcfefdd9-301e-001a-2135-3a23d2000000
    // 	Time:2023-02-06T14:17:00.7805577Z
    //     </Message>
    // </Error>

    print("\(sb.getAsString()!)")

    // To see if we got Tags or an Error, then examine the XML.
    let xml = CkoXml()!
    xml.loadSb(sb: sb, autoTrim: true)

    // Examine the tag..
    if xml.tagEquals(tag: "Tags") == true {
        print("Blob exists!")
    }
    else {

        if xml.childContentMatches(tagPath: "Code", pattern: "BlobNotFound", caseSensitive: true) == true {
            print("Blob does not exist.")
        }
        else {
            print("Some other error...")
        }

    }

    print("Success.")

}