【问题标题】:Upload all files in folder directory to Google Cloud Storage without loop将文件夹目录中的所有文件无循环上传到 Google Cloud Storage
【发布时间】:2018-12-05 07:23:58
【问题描述】:

如何将文件夹中的所有文件无循环上传到 Google Storage Cloud。 在这里,我尝试使用 C# 上传一个文件:

public async Task<ActionResult> Upload(IFormFile file)
{
    string contentRootPath = _hostingEnvironment.ContentRootPath;

    var credential = GoogleCredential.FromJson(System.IO.File.ReadAllText(contentRootPath + "/credentials.json"));

    StorageClient storageClient = StorageClient.Create(credential);

    var objectName = MakeObjectName();

    var imageObject = await storageClient.UploadObjectAsync(
        bucket: CloudConfig.BUCKET,
        objectName: objectName,
        contentType: file.ContentType,
        source: file.OpenReadStream(),
        options: new UploadObjectOptions { PredefinedAcl = PredefinedObjectAcl.PublicRead }
    );

    return Ok(new DropzoneInfo { Name = file.FileName, ObjectName = objectName, Link = imageObject.MediaLink, Size = file.Length});
}

【问题讨论】:

  • 你显示的代码有问题吗?
  • 代码没问题。仅作为示例。
  • 那你能详细说明一下这个扩展到多个文件的问题吗?
  • 我只是想对块上传到谷歌云存储的代码提出质疑。并非所有代码都在行动中。
  • 您的问题到底是什么?目前我看到了几种可能性:a)您想知道如何获取目录中的文件列表。 b) 你想知道如何循环上传每个文件的列表。

标签: c# google-cloud-storage


【解决方案1】:

您有一个接受单个上传文件的控制器操作。在您的控制器操作中使用 List&lt;IFormFile&gt; - 请参阅此答案:Submitting multiple files to ASP.NET controller accepting an ICollection<IFormFile>

上传目录有一个属性:&lt;input type="file" webkitdirectory&gt;(How do I use Google Chrome 11's Upload Folder feature in my own code?)

这些浏览器支持它:Edge、Safari、Firefox、Chrome、Opera (https://caniuse.com/#feat=input-file-directory)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
  • 1970-01-01
  • 2021-11-08
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
相关资源
最近更新 更多