Matt
Matt

Reputation: 21

Setting an object to Shared Public URL when inserting into Google Cloud Storage using c#

I am uploading an MP3 file to a bucket in google cloud storage, but would like to make the inserted file publicly available and grab the url for it.

Below is my current code, but i cannot figure how to set the permission on the object after it is uploaded to make it publicly available.

Google.Apis.Storage.v1.Data.Object fileobj = new Google.Apis.Storage.v1.Data.Object() { Name = "recording.mp3" };

            Console.WriteLine("Creating " + fileobj.Name + " in bucket " + bucketName);

            byte[] file = File.ReadAllBytes(@"C:\Users\test\Desktop\stream\recording.mp3");

            using (MemoryStream memory = new MemoryStream(file))
            {
                using (BinaryReader reader = new BinaryReader(memory))
                {
                    for (int i = 0; i < file.Length; i++)
                    {
                        byte result = reader.ReadByte();
                        Console.WriteLine(result);
                    }
                }
            }

            using (var streamOut = new MemoryStream(file))
            {
                await service.Objects.Insert(fileobj, bucketName, streamOut, "audio/mpeg3").UploadAsync();
            }

Upvotes: 1

Views: 760

Answers (1)

user1435381
user1435381

Reputation: 31

Add to fileobj:

 Acl = new List<ObjectAccessControl>
                {
                    new ObjectAccessControl
                    {
                        Role = "OWNER",
                        Entity = "allUsers"
                    }
                }

Upvotes: 3

Related Questions