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.
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.