简体   繁体   English

创建文件夹 google cloud storage bucket .NET Client Library

[英]Create folder google cloud storage bucket .NET Client Library

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我正在寻找一种使用以下客户端库在存储桶内创建文件夹的方法: 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 使用php在存储桶谷歌云存储中创建文件夹

I can't see any way of being able to specify a uploadtype , all requests appear to be uploadType=resumable .我看不到任何能够指定uploadtype ,所有请求似乎都是uploadType=resumable

Looking at the above post and a fiddler trace I need to use uploadType=media .查看上面的帖子和提琴手跟踪,我需要使用uploadType=media Is there away to accomplish this?有没有办法做到这一点?

It is a bit more straighforward to do this in the latest version of the API.在最新版本的 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));

        }

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.编辑:刚刚发现您可以直接将文件上传到目标对象,如果目录/子目录不存在,上传功能将为您创建它们。

All you need to do is put the folder(s) you want before the file name您需要做的就是将所需的文件夹放在文件名之前

using (MemoryStream ms = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(json)))
                        {
                            string fileName = $"/test/somefolder/{sourceId}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.json";
                            await gcpClient.UploadObjectAsync(gcpBucketName, fileName, null, ms);
                        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM