Create folder google cloud storage bucket .NET Client Library

浪子不回头ぞ 提交于 2020-02-08 03:22:33

问题


I'm looking to at a way to create a folder inside a bucket using the following client library: https://cloud.google.com/storage/docs/json_api/v1/json-api-dotnet-samples

I've looked at the following thread, and it appears I need to use the simple method upload. Creating folder in bucket google cloud storage using php

I can't see any way of being able to specify a uploadtype, all requests appear to be uploadType=resumable.

Looking at the above post and a fiddler trace I need to use uploadType=media. Is there away to accomplish this?


回答1:


It is a bit more straighforward to do this in the latest version of the API. You still have to make sure that you check for the "/" as Salem has suggested.

     public void AddFolder(string folder)
        {
           StorageClient storageClient = StorageClient.Create();
           if (!FolderName.EndsWith("/"))
             FolderName += "/";         

          var content = Encoding.UTF8.GetBytes("");

           storageClient.UploadObject(bucketName, folder, "application/x-directory", new MemoryStream(content));

        }



回答2:


This worked for me! Don't know if it's the right way to do it, but there are not enough on this.

public void CreateDir(string FolderName)
    {
        if (!FolderName.EndsWith("/"))
            FolderName += "/";

        var uploadStream = new MemoryStream(Encoding.UTF8.GetBytes(""));
        Storage.Objects.Insert(
            bucket: BucketName,
            stream: uploadStream,
            contentType: "application/x-directory",
            body: new Google.Apis.Storage.v1.Data.Object() { Name = FolderName}
            ).Upload();
    }

EDIT: Just found out that you can directly upload the file to your destination objects, and if the directories/sub-directories don't exist the upload function will create them for you.



来源:https://stackoverflow.com/questions/33209663/create-folder-google-cloud-storage-bucket-net-client-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!