英文:
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<IDownloadProgress> 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("notification-email-layout", "Templates/Email/test.html", stream);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论