Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.6.0+

Azure Blob: Upload BinData to a Block Blob

See more Azure Cloud Storage Examples

Upload bytes already held in a BinData object. LoadFile is checked before any upload.

If-None-Match: * makes creation fail with 412 when the blob exists. Remove the condition only for an intentional complete replacement; Put Blob overwrites content and metadata. With the selected API version, a single Put Blob supports up to 5,000 MiB, not the historical 64 MiB limit. Memory-based examples should use small inputs; use streaming or staged blocks for larger data.

Uses direct Chilkat REST calls with HTTPS certificate verification. Shared Key examples load a private qa_data/azure/storage.json file containing string fields account and access_key (the base64 account key). SAS consumers need only account there. Create local input/output directories before running. Examples target Azure public-cloud endpoints; adapt hostnames for other clouds or custom endpoints.

REST examples pin x-ms-version: 2026-04-06, and SAS generation uses the same service version. Complete-URL downloads use the supplied SAS version. This is a fully deployed version documented by Microsoft at review time. Older supported versions are not automatically obsolete. Transport completion and HTTP success are checked separately; writes are not automatically replayed after a lost response. Keep diagnostic dumps, keys and SAS URLs private.

Shared Key remains supported where allowed by account policy. Microsoft recommends Microsoft Entra ID, preferably managed identities where available, and user delegation SAS for delegated Blob access. Account-key SAS and Shared Key requests can be rejected when Shared Key access is disabled; these samples do not bypass that policy. See Azure data authorization.

Microsoft put blob reference.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  StringBuilder compares text by value in every generated language.
    let textCompare = CkoStringBuilder()!
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false
    let config = CkoJsonObject()!
    config.strict = true
    success = config.loadFile(path: "qa_data/azure/storage.json")
    if success != true {
        print("Could not load private Azure configuration.")
        return
    }

    textCompare.clear()
    textCompare.setString(value: config.string(of: "account"))
    var sameText1: Bool = textCompare.contentsEqual(str: "", caseSensitive: true)
    if (config.jsonType(of: "account").intValue != 1) || (sameText1 == true) {
        print("Supply the storage account name.")
        return
    }

    //  access_key is the base64 account key, not a connection string or SAS.
    textCompare.clear()
    textCompare.setString(value: config.string(of: "access_key"))
    var sameText2: Bool = textCompare.contentsEqual(str: "", caseSensitive: true)
    if (config.jsonType(of: "access_key").intValue != 1) || (sameText2 == true) {
        print("Supply the base64 account access key.")
        return
    }

    let endpoint = CkoStringBuilder()!
    success = endpoint.append(value: config.string(of: "account"))
    if success != true {
        print("Could not prepare the request.")
        return
    }

    success = endpoint.append(value: ".blob.core.windows.net")
    if success != true {
        print("Could not prepare the request.")
        return
    }

    //  resourcePath is already URL-encoded. Encode each path segment once when adapting it.
    //  Keep slash separators; names and paths are case-sensitive.
    var resourcePath: String? = "/examplecontainer/example.bin"
    let rest = CkoRest()!
    rest.connectTimeoutMs = 20000
    rest.idleTimeoutMs = 60000
    let azAuth = CkoAuthAzureStorage()!
    azAuth.account = config.string(of: "account")
    azAuth.accessKey = config.string(of: "access_key")
    azAuth.service = "Blob"
    azAuth.scheme = "SharedKey"
    azAuth.xMsVersion = "2026-04-06"
    success = rest.setAuthAzureStorage(authProvider: azAuth)
    if success != true {
        print("Could not configure Shared Key signing.")
        return
    }

    let connection = CkoSocket()!
    connection.requireSslCertVerify = true
    connection.maxReadIdleMs = 60000
    connection.maxSendIdleMs = 60000
    success = connection.connect(hostname: endpoint.getAsString(), port: 443, ssl: true, maxWaitMs: 20000)
    if success != true {
        print("TLS connection failed; inspect diagnostics privately.")
        return
    }

    //  Do not replay a write automatically after losing its response.
    success = rest.useConnection(connection: connection, autoReconnect: false)
    if success != true {
        print("Could not prepare the request.")
        return
    }

    let responseBody = CkoStringBuilder()!
    let result = CkoXml()!
    result.strict = true
    success = rest.addHeader(name: "x-ms-blob-type", value: "BlockBlob")
    if success != true {
        print("Could not prepare the request.")
        return
    }

    success = rest.addHeader(name: "If-None-Match", value: "*")
    if success != true {
        print("Could not prepare the request.")
        return
    }

    let data = CkoBinData()!
    success = data.loadFile(path: "qa_data/bin/sample.bin")
    if success != true {
        print("Could not read local input.")
        return
    }

    responseBody.clear()
    success = rest.fullRequestBd(httpVerb: "PUT", uriPath: resourcePath, binData: data, responseBody: responseBody)
    if success != true {
        print("Request did not complete; outcome may be uncertain.")
        return
    }

    if rest.responseStatusCode.intValue != 201 {
        print("HTTP status: \(rest.responseStatusCode.intValue)")
        print("Azure error code: \(rest.responseHdr(byName: "x-ms-error-code")!)")
        print("Request ID: \(rest.responseHdr(byName: "x-ms-request-id")!)")
        //  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
        return
    }

    print("Blob created.")
    print("ETag: \(rest.responseHdr(byName: "ETag")!)")

}