Uploading Objects to Google Cloud Storage Buckets in C#

Upload File Directly to Google Cloud Storage from user's webpage

There are lots of examples on the Internet in most languages including JavaScript. Google search and study how this is done. You are possibly choosing the worst environment for accomplishing your goal (JavaScript in a Browser). Some companies spend months trying to get the browser interface and to upload objects correctly when they need absolutely clean code (no copyright issues) AND need to handle all the problems that the public Internet causes worldwide.

From a design / security view I will give you some tips:

  1. You will need to use signed-urls. This prevents leaking credentials. This is a pain to get correct for browser-based uploads. The alternatives are to open the bucket to the public which is VERY DANGEROUS or to use an intermediary such as Google Compute Engine.
  2. Providing this service will cost you money. At this time network egress starts at $0.12 per GB and goes up depending on the region. This price will drop in the third/fourth quarter of 2019 to $0.105 per GB.
  3. You mentioned sending a link. You will need a form for the user to enter the email address. Then you need to store that email address in a database such as Cloud Datastore.
  4. Once the upload completes, you need logic to connect the file upload with the email address stored in step #3. Cloud Storage supports triggers so that a backed service can be notified that the file transfer is complete. You then map the email address to the Cloud Storage object and send an email. Use Cloud Pub/Sub to receive notifications from Cloud Storage and then call either Cloud Functions or Cloud Run to process and fire off the email.

Good luck with this project. Getting a prototype working is not that difficult. Handling all the real world issues, browser incompatibilities, mobile (iOS and Android), etc. will drive you nuts.

C# Google Storage Client - Upload file - Fail if already exists

You can prevent the overwrite using ifGenerationMatch(0):

https://cloud.google.com/storage/docs/json_api/v1/objects/insert#parameters

I am not a C# expert, but I think in C# this is available via the UploadObjectOptions:

https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Storage.V1/api/Google.Cloud.Storage.V1.UploadObjectOptions.html

Specifically:

https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Storage.V1/api/Google.Cloud.Storage.V1.UploadObjectOptions.html#Google_Cloud_Storage_V1_UploadObjectOptions_IfGenerationMatch

Google Cloud Storage api (c#) - cache header metadata

You can set the cache control when you call UploadObject, if you specify an Object instead of just the bucket name and object name. Here's an example:

var client = StorageClient.Create();
var obj = new Google.Apis.Storage.v1.Data.Object
{
Bucket = bucketId,
Name = objectName,
CacheControl = "public,max-age=3600"
};
var stream = new MemoryStream(Encoding.UTF8.GetBytes("Hello world"));
client.UploadObject(obj, stream);

You can do it after the fact as well using PatchObject:

var patch = new Google.Apis.Storage.v1.Data.Object
{
Bucket = bucketId,
Name = objectName,
CacheControl = "public,max-age=7200"
};
client.PatchObject(patch);

I don't know about the details of cache control if you haven't specified anything though, I'm afraid.

Google Cloud Storage: Deleting contents of a bucket

I suggest using the C# client libraries. With these, deleting the files within a bucket could be achieved by using the following code:

using System;
using Google.Cloud.Storage.V1;

namespace csharp {
public class deletingFilesFromBucket
{
static void Main(string[] args)
{
var storage = StorageClient.Create();
var bucketName = "MyBucket"
foreach (var storageObject in storage.ListObjects(bucketName, ""))
{
storage.DeleteObject(bucketName, storageObject.Name);
Console.WriteLine($"Deleted {storageObject.Name}.");
}
}
}
}

You could find the reference for this in this document.



Related Topics



Leave a reply



Submit