从谷歌云存储桶中的文件夹获取文件内容

huangapple go评论40阅读模式
英文:

Get file contents from file in folder on google cloud storage bucket

问题

我想获取位于Google存储桶中文件夹内的文件内容。

当我尝试使用以下方式获取文件夹内的文件时:

// storage.DownloadObject("notification-email-layout/Templates/Email/", "DefaultTemplate.html", stream);

我会收到异常信息:"System.ArgumentException: 'Invalid bucket name 'notification-email-layout/Templates/Email/'"

如果我尝试获取存储桶中的文件而不是存储桶内的文件夹中的文件,它可以正常工作。

-- 这段代码可以正常工作

var storage = StorageClient.Create();
Stream stream = new MemoryStream();
storage.DownloadObject("notification-email-layout", "test.html", stream);
StreamReader sr = new StreamReader(stream);
sr.BaseStream.Position = 0;
string txt = "";
while (!sr.EndOfStream)
{
    txt += sr.ReadLine();
}
sr.Close();

return Content(txt, "text/html");

我如何获取存储桶内文件夹中的文件内容?

英文:

I want to get the contents of a file that resides within a folder in a google storage bucket.

When I try to use the following to get a file in a folder:
// storage.DownloadObject("notification-email-layout/Templates/Email/", "DefaultTemplate.html", stream);

I get an exception: "System.ArgumentException: 'Invalid bucket name 'notification-email-layout/Templates/Email/' "

If I try to hit a file in just the bucket and not in a folder within that bucket it works

--This code works

 var storage = StorageClient.Create();
        Stream stream = new MemoryStream();
        storage.DownloadObject("notification-email-layout", "test.html", stream);
        StreamReader sr = new StreamReader(stream);
        sr.BaseStream.Position= 0;
        string txt = "";
        while (!sr.EndOfStream)
        {
            txt += sr.ReadLine();
        }
        sr.Close();

        return Content(txt, "text/html");

How can I get the contents of a file which resides in a folder within a google storage bucket?

答案1

得分: 0

请查看 DownloadObject 的 API 文档:

public virtual Object DownloadObject(
    string bucket,
    string objectName,
    Stream destination,
    DownloadObjectOptions options = null,
    IProgress<IDownloadProgress> progress = null
)

第一个参数始终是桶的名称,而不是桶内对象的路径。第二个参数是对象的路径。

如果桶的名称是 "notification-email-layout",对象的路径是 "Templates/Email/test.html",您可能希望这样编写:

storage.DownloadObject("notification-email-layout", "Templates/Email/test.html", stream);
英文:

Look at the API documentation for DownloadObject:

public virtual Object DownloadObject(
    string bucket,
    string objectName,
    Stream destination,
    DownloadObjectOptions options = null,
    IProgress&lt;IDownloadProgress&gt; progress = null
)

The first argument is always the name of the bucket, not a path to an object within a bucket. The second argument is the path to the object.

You probably want to write this instead, if the name of the bucket is "notification-email-layout" and the path to the object is "Templates/Email/test.html":

storage.DownloadObject(&quot;notification-email-layout&quot;, &quot;Templates/Email/test.html&quot;, stream);

huangapple
  • 本文由 发表于 2023年2月16日 06:50:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定