使用 c# 将文件上传到谷歌云存储

Upload files to Google cloud storage using c#(使用 c# 将文件上传到谷歌云存储)
本文介绍了使用 c# 将文件上传到谷歌云存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下示例代码将文件上传到 Google 云存储(存储桶).从我的本地机器执行时它工作正常.但是在生产环境中执行时给了我以下异常.程序代码.

I am using the below sample code to upload the file into the Google cloud storage (Bucket). it is working properly when executed from my local machine. But is giving me the following exception when executed in the production environment. Program code.

public class Program
{
    static void Main(string[] args)
    {
        try
        {
            string bucketName = "bucketName";
            string sharedkeyFilePath = GetFilePath("privateKey.json");
            GoogleCredential credential = null;
            using (var jsonStream = new FileStream(sharedkeyFilePath, FileMode.Open,
                FileAccess.Read, FileShare.Read))
            {
                credential = GoogleCredential.FromStream(jsonStream);
            }
            var storageClient = StorageClient.Create(credential);
            string filetoUpload = GetFilePath("demo.txt");
            using (var fileStream = new FileStream(filetoUpload, FileMode.Open,
                FileAccess.Read, FileShare.Read))
            {
                storageClient.UploadObject(bucketName, "demo.txt", "text/plain", fileStream);
            }
            Console.WriteLine("uploaded the file successfully");
            Console.ReadLine();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public static string GetFilePath(string filename)
    {
        return GetFilePath(Assembly.GetCallingAssembly().Location, filename);
    }
    public static string GetFilePath(string path, string filename)
    {
        return path.Substring(0, path.LastIndexOf("\")) + @"" + filename;
    }
}

异常详情:

`A task was canceled.
  at Google.Cloud.Storage.V1.StorageClientImpl.UploadHelper.CheckFinalProgress()
   at Google.Cloud.Storage.V1.StorageClientImpl.UploadHelper.Execute()
   at Google.Cloud.Storage.V1.StorageClientImpl.UploadObject(Object destination, Stream source, UploadObjectOptions options, IProgress`1 progress)
   at Google.Cloud.Storage.V1.StorageClientImpl.UploadObject(String bucket, String objectName, String contentType, Stream source, UploadObjectOptions options, IProgress`1 progress)
   at gcsUpload.Program.Main(String[] args) in d:Ganesh DeshmukhProjectsGCSgcsUploadgcsUploadProgram.cs:line 49
Google.Cloud.Storage.V1`

推荐答案

防火墙设置有问题.上面的代码工作正常.

There was an issue with firewall settings. Above code work properly.

这篇关于使用 c# 将文件上传到谷歌云存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)