public async static Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { try { log.LogInformation("C# HTTP trigger function starts process a request."); log.LogInformation("Request is a " + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); string containerName = req.Query["containername"]; if (containerName == null) { string errorMessage = "No container name given!"; log.LogError(errorMessage); return(new BadRequestObjectResult(errorMessage)); } CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Cryptdrive.AzureLinkStringStorage.STORAGE_CONNECTION_STRING); var client = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = BlobManager.getBlobContainer(containerName); bool isFormType = req.HasFormContentType; int numberOfDeletedFiles = 0; var keys = req.Form.Keys; foreach (var key in keys) { StringValues returns; req.Form.TryGetValue(key, out returns); log.LogInformation("Trying to delete " + returns); log.LogInformation(" From key " + key); bool deleted = await container.GetBlockBlobReference(returns).DeleteIfExistsAsync(); if (deleted) { numberOfDeletedFiles++; } } return(new OkObjectResult(numberOfDeletedFiles + " files deleted from the cloud")); } catch (Exception e) { log.LogError(e.Message); log.LogError(e.StackTrace); return(new UnprocessableEntityObjectResult(e.Message)); } }
public static async System.Threading.Tasks.Task <IActionResult> RunAsync([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { try { log.LogInformation("C# HTTP trigger function processed a request."); log.LogInformation("Request is a " + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); string containerName = req.Query["container"]; if (containerName == null) { string errorMessage = "No container name given!"; log.LogError(errorMessage); return(new BadRequestObjectResult(errorMessage)); } string filenameOld = req.Query["filenameOld"]; if (filenameOld == null) { string errorMessage = "No old file name given!"; log.LogError(errorMessage); return(new BadRequestObjectResult(errorMessage)); } string filenameNew = req.Query["filenameNew"]; if (filenameNew == null) { string errorMessage = "No new file name given!"; log.LogError(errorMessage); return(new BadRequestObjectResult(errorMessage)); } log.LogInformation("Got the names:" + filenameOld + "->" + filenameNew + " in " + containerName); CloudBlobContainer container = BlobManager.getBlobContainer(containerName); // Based on https://github.com/Azure-Samples/storage-blob-dotnet-getting-started/blob/master/BlobStorage/Advanced.cs function CopyBlockBlobAsync var sourceBlob = container.GetBlockBlobReference(filenameOld);; var destBlob = container.GetBlockBlobReference(filenameNew); string leaseId = null; log.LogInformation("Got the container references"); try { // Lease the source blob for the copy operation to prevent another client from modifying it. // Specifying null for the lease interval creates an infinite lease. leaseId = await sourceBlob.AcquireLeaseAsync(null); // Ensure that the source blob exists. if (await sourceBlob.ExistsAsync()) { // Get the ID of the copy operation. string copyId = await destBlob.StartCopyAsync(sourceBlob); // Fetch the destination blob's properties before checking the copy state. await destBlob.FetchAttributesAsync(); log.LogInformation("Status of copy operation: {0}", destBlob.CopyState.Status); log.LogInformation("Completion time: {0}", destBlob.CopyState.CompletionTime); log.LogInformation("Bytes copied: {0}", destBlob.CopyState.BytesCopied.ToString()); log.LogInformation("Total bytes: {0}", destBlob.CopyState.TotalBytes.ToString()); await releaseLease(sourceBlob); log.LogInformation("Released the lease, now comes the deleting..."); //delete old file after copy is done await sourceBlob.DeleteIfExistsAsync(); log.LogInformation("... file deleted, rename finished"); } else { log.LogError("Source not found!"); } } catch (StorageException e) { log.LogError(e.Message); throw; } finally { await releaseLease(sourceBlob); } return((ActionResult) new OkObjectResult($"Hello, ")); } catch (Exception e) { log.LogError(e.Message); log.LogError(e.StackTrace); return(new UnprocessableEntityObjectResult(e.Message)); } }
public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { try { log.LogInformation("C# HTTP trigger function starts process a request."); log.LogInformation("Request is a " + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); string containerName = req.Query["username"]; if (containerName == null) { string errorMessage = "No container name given!"; log.LogError(errorMessage); return(new BadRequestObjectResult(errorMessage)); } string fileName = req.Query["filename"]; if (fileName == null) { string errorMessage = "No file name given!"; log.LogError(errorMessage); return(new BadRequestObjectResult(errorMessage)); } if (req.ContentLength == null || req.ContentLength == 0) { return(new BadRequestObjectResult("Could not see raw data")); } else { log.LogInformation(@"Uploading {req.ContentLength} bytes"); } if (req.ContentLength > int.MaxValue) { return(new BadRequestObjectResult("Content to upload is too large")); } int sizeOfBlobsInContainer = await BlobManager.getSizeInBytes(containerName); int remainingSize = BlobManager.maxSizeOfBlobForUser - sizeOfBlobsInContainer; if (req.ContentLength > remainingSize) { log.LogError("User tried to upload too much into his container"); return(new UnauthorizedResult()); } else { log.LogInformation("User has enough free space"); } byte[] data = new byte[(int)req.ContentLength]; req.Body.Read(data, 0, (int)req.ContentLength); log.LogInformation("Reading container name"); CloudBlobContainer container = BlobManager.getBlobContainer(containerName); // Upload a BlockBlob to the newly created container log.LogInformation("Uploading BlockBlob"); CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); await blockBlob.UploadFromByteArrayAsync(data, 0, data.Length); log.LogInformation("C# HTTP trigger function finish process a request."); return(new OkObjectResult($"Uploaded {fileName} to {containerName} which had a size of {req.ContentLength}")); } catch (Exception e) { log.LogError(e.Message); log.LogError(e.StackTrace); return(new UnprocessableEntityObjectResult(e.Message)); } }