Integrate Azure Blob storage in SwiftUI project

Sandhya Raut 10 Reputation points
2025-03-26T15:46:13.6433333+00:00

Does anyone has an example code for integrating Azure blob storage in Swift?

Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,539 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Keshavulu Dasari 4,840 Reputation points Microsoft External Staff Moderator
    2025-04-01T19:00:35.6033333+00:00

    Hi Sandhya Raut ,

    To integrate Azure Blob Storage in your SwiftUI project using a SAS token, you can follow these steps:

    You need to generate a SAS token for your Azure Blob Storage. This token allows you to upload files securely without exposing your storage account keys.

    While you mentioned having issues with the Azure SDK for iOS, you can use the @azure/storage-blob package in a JavaScript context to understand how to work with SAS tokens. Unfortunately, there are no direct examples in Swift provided in the context, but the general approach would be similar.

    Once you have the SAS token, you can use it to upload files directly to Azure Blob Storage. The typical flow involves:

    • Selecting a file from the device.
    • Using the SAS token to construct the upload URL.
    • Making an HTTP request to upload the file.

    Just example of how you might structure your upload function in Swift:

    
    func uploadFileToAzureBlob(fileURL: URL, sasToken: String) {
        let storageAccountName = "YOUR_STORAGE_ACCOUNT_NAME"
        let containerName = "YOUR_CONTAINER_NAME"
        let fileName = fileURL.lastPathComponent
        let uploadURL = "https://\(storageAccountName).blob.core.windows.net/\(containerName)/\(fileName)?\(sasToken)"
        
        var request = URLRequest(url: URL(string: uploadURL)!)
        request.httpMethod = "PUT"
        request.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
        
        let fileData = try? Data(contentsOf: fileURL)
        request.httpBody = fileData
        
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            if let error = error {
                print("Error uploading file: \(error)")
                return
            }
            print("File uploaded successfully!")
        }
        
        task.resume()
    }
    

    Make sure to replace YOUR_STORAGE_ACCOUNT_NAME and YOUR_CONTAINER_NAME with your actual Azure Blob Storage account name and container name.

    For more detailed guidance, you might want to refer to Azure's documentation on uploading files to Blob Storage using SAS tokens.

    https://learn.microsoft.com/en-us/azure/developer/javascript/tutorial/browser-file-upload-azure-storage-blob?tabs=github-codespaces

    https://learn.microsoft.com/en-us/azure/developer/javascript/tutorial/browser-file-upload-azure-storage-blob?tabs=github-codespaces#application-architecture

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members. 
    User's image

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.