Skip to content

Instantly share code, notes, and snippets.

@danielkon96
Last active September 20, 2019 23:41
Show Gist options
  • Save danielkon96/df11120d47e751f8eb4bd949e4ec4c04 to your computer and use it in GitHub Desktop.
Save danielkon96/df11120d47e751f8eb4bd949e4ec4c04 to your computer and use it in GitHub Desktop.
Upload Images to Azure Blob Storage
private async void UploadImagesButton_Clicked(object sender, EventArgs e)
{
// Get the list of images we have selected.
List<string> imagePaths = ImgCarouselView.ItemsSource as List<string>;
// If user is using Android, compress the images. (Optional)
if (Device.RuntimePlatform == Device.Android)
{
imagePaths = CompressAllImages(imagePaths);
}
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=your_account_name_here;AccountKey=your_account_key_here");
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("multiimagepickercontainer");
// Create the container if it doesn't already exist.
await container.CreateIfNotExistsAsync();
// Set the container access permissions (Optional)
BlobContainerPermissions blobPermissions = new BlobContainerPermissions();
blobPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;
await container.SetPermissionsAsync(blobPermissions);
// Change info text. (Optional)
InfoText.Text = "Uploading to Azure Blob Storage...";
// Upload all the selected images to Azure Blob Storage.
int count = 1;
foreach (string img in imagePaths)
{
// Retrieve reference to a blob named "newphoto#.jpg".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("newphoto" + count + ".jpg");
// Create the "newphoto#.jpg" blob with the current image in our list.
await blockBlob.UploadFromFileAsync(img);
count++;
}
// Change info text. (Optional)
InfoText.Text = "Upload complete.";
// Clear temp files after upload.
if (Device.RuntimePlatform == Device.Android)
{
DependencyService.Get<IMediaService>().ClearFileDirectory();
}
if (Device.RuntimePlatform == Device.iOS)
{
GMMultiImagePicker.Current.ClearFileDirectory();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment