有人可以请提供一个如何使用谷歌.Apis.Storage.v1上传文件到谷歌云存储在c#中的例子吗?上传对象到谷歌云存储桶在c#
1
A
回答
6
我发现这个基本操作并不像你所期望的那么简单。 Google关于它的Storage API的文档缺乏有关在C#(或任何其他.NET语言)中使用它的信息。搜索“如何上传文件到谷歌云存储在C#”并没有完全帮助我,所以这里有一些意见我工作的解决方案:
准备:
您需要在您的Google Developers Console中创建OAuth2帐户 - 转至项目/ API & auth/Credentials。
复制客户端ID &客户端密码到您的代码。您还需要您的项目名称。
代码(它假定您已经通过的NuGet添加Google.Apis.Storage.v1):
首先,您需要授权您的要求:
var clientSecrets = new ClientSecrets();
clientSecrets.ClientId = clientId;
clientSecrets.ClientSecret = clientSecret;
//there are different scopes, which you can find here https://cloud.google.com/storage/docs/authentication
var scopes = new[] {@"https://www.googleapis.com/auth/devstorage.full_control"};
var cts = new CancellationTokenSource();
var userCredential = await GoogleWebAuthorizationBroker.AuthorizeAsync(clientSecrets,scopes, "[email protected]", cts.Token);
有时您可能还希望通过以下方式刷新授权令牌:
await userCredential.RefreshTokenAsync(cts.Token);
您还需要创建存储服务:
var service = new Google.Apis.Storage.v1.StorageService();
现在您可以向Google Storage API发送请求。 让我们先从创建一个新的水桶:
var newBucket = new Google.Apis.Storage.v1.Data.Bucket()
{
Name = "your-bucket-name-1"
};
var newBucketQuery = service.Buckets.Insert(newBucket, projectName);
newBucketQuery.OauthToken = userCredential.Result.Token.AccessToken;
//you probably want to wrap this into try..catch block
newBucketQuery.Execute();
它完成。现在,您可以发送一个请求,让所有的桶的列表:
var bucketsQuery = service.Buckets.List(projectName);
bucketsQuery.OauthToken = userCredential.Result.Token.AccessToken;
var buckets = bucketsQuery.Execute();
最后一部分是上传新文件:
//enter bucket name to which you want to upload file
var bucketToUpload = buckets.Items.FirstOrDefault().Name;
var newObject = new Object()
{
Bucket = bucketToUpload,
Name = "some-file-"+new Random().Next(1,666)
};
FileStream fileStream = null;
try
{
var dir = Directory.GetCurrentDirectory();
var path = Path.Combine(dir, "test.png");
fileStream = new FileStream(path, FileMode.Open);
var uploadRequest = new Google.Apis.Storage.v1.ObjectsResource.InsertMediaUpload(service, newObject,
bucketToUpload,fileStream,"image/png");
uploadRequest.OauthToken = userCredential.Result.Token.AccessToken;
await uploadRequest.UploadAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (fileStream != null)
{
fileStream.Dispose();
}
}
和BAM!新文件将显示在您选择的存储区内的Google Developers Console中。
1
你会很高兴知道它仍然在2016年... 我是谷歌搜索使用像“谷歌gcp C#上传图像”的花式关键词,直到我只是普通的问题:“我如何上传图片到谷歌存储桶使用C#“...和我在这里。我删除了用户凭证中的.Result
,这是我为之工作的最终编辑。
// ******
static string bucketForImage = ConfigurationManager.AppSettings["testStorageName"];
static string projectName = ConfigurationManager.AppSettings["GCPProjectName"];
string gcpPath = Path.Combine(Server.MapPath("~/Images/Gallery/"), uniqueGcpName + ext);
var clientSecrets = new ClientSecrets();
clientSecrets.ClientId = ConfigurationManager.AppSettings["GCPClientID"];
clientSecrets.ClientSecret = ConfigurationManager.AppSettings["GCPClientSc"];
var scopes = new[] { @"https://www.googleapis.com/auth/devstorage.full_control" };
var cts = new CancellationTokenSource();
var userCredential = await GoogleWebAuthorizationBroker.AuthorizeAsync(clientSecrets, scopes, ConfigurationManager.AppSettings["GCPAccountEmail"], cts.Token);
var service = new Google.Apis.Storage.v1.StorageService();
var bucketToUpload = bucketForImage;
var newObject = new Google.Apis.Storage.v1.Data.Object()
{
Bucket = bucketToUpload,
Name = bkFileName
};
FileStream fileStream = null;
try
{
fileStream = new FileStream(gcpPath, FileMode.Open);
var uploadRequest = new Google.Apis.Storage.v1.ObjectsResource.InsertMediaUpload(service, newObject,
bucketToUpload, fileStream, "image/"+ ext);
uploadRequest.OauthToken = userCredential.Token.AccessToken;
await uploadRequest.UploadAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (fileStream != null)
{
fileStream.Dispose();
}
}
// ******
相关问题
- 1. 在谷歌云存储桶上上传大文件
- 2. 谷歌云存储:PUT对象与POST对象上传文件。
- 3. 无法读取上传到谷歌云存储存储桶中的csv文件
- 4. 如何从谷歌云存储桶下载对象?
- 5. 谷歌存储桶上传权限
- 6. 谷歌云存储桶创建
- 7. 谷歌云存储桶内容
- 8. 谷歌云存储无法检索桶
- 9. 谷歌云存储桶备份/恢复
- 10. 在谷歌云存储桶/对象上设置“域”权限 - 仅限UI或API?
- 11. 谷歌云存储 - 知道谁上传
- 12. 谷歌云存储卷曲php上传
- 13. 谷歌云存储最大对象
- 14. 谷歌云平台 - 如何将图像文件上传到谷歌云存储桶?
- 15. 在谷歌云存储桶上部署angular2 web应用程序
- 16. 使用SSIS将本地文件上传到谷歌云存储桶
- 17. 插入对象到谷歌云存储返回'没有这样的桶'
- 18. 谷歌pubsub到谷歌云存储
- 19. 限制存储桶大小谷歌云存储
- 20. 谷歌云存储
- 21. 谷歌云存储桶上的公开共享文件
- 22. 测试谷歌云存储桶是否存在?
- 23. 输入/输出错误写入到谷歌云存储桶时
- 24. 上传到谷歌云存储从Django的App Engine上
- 25. 文件上传到谷歌云存储AppEngine上(Python)的
- 26. 设置个人资料图片谷歌云存储桶在Android
- 27. 在没有谷歌存储桶的情况下利用谷歌的云CDN
- 28. 安装谷歌云存储桶,但本地缓存
- 29. 上传到谷歌云存储中的Android
- 30. 如何使用java将文件上传到谷歌云存储?
Upvote for for out the Object nature:new Google.Apis.Storage.v1.Data.Object – swissben 2018-03-08 12:39:16