简体   繁体   English

使用服务帐户将文件上传到Google Merchant Center中生成的Google Cloud Storage存储桶

[英]Upload file using Service Account to Google Cloud Storage bucket that was generated in Google Merchant Center

I was able to upload a file to the Google Cloud Storage bucket with the following code. 我可以使用以下代码将文件上传到Google Cloud Storage存储桶。 I created the bucket manually. 我手动创建了存储桶。

        String serviceAccountEmail = "xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com";

        var certificate = new X509Certificate2(@"C:\...\projectname-xxxxxxxxxxxx.p12", "notasecret", X509KeyStorageFlags.Exportable);
        var scopes = new[] { @"https://www.googleapis.com/auth/devstorage.full_control" };

        ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                Scopes = scopes
            }.FromCertificate(certificate));

        StorageService service = new StorageService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Shipment Feed",
        });

        var bucketToUpload = "bucket-created-by-me";
        var newObject = new Google.Apis.Storage.v1.Data.Object()
        {
            Bucket = bucketToUpload,
            Name = "ShipmentFeed.txt"
        };


        FileStream fileStream = null;
        var path = @"C:\...\file.txt";
        fileStream = new FileStream(path, FileMode.Open);
        var uploadRequest = new ObjectsResource.InsertMediaUpload(service, newObject,
           bucketToUpload, fileStream, "text/plain");
        uploadRequest.Upload();

Now I ran into the problem when I tried to upload the file to the bucket, generated by Google Merchant Center, for the same Google account. 现在,当我尝试将文件上传到Google Merchant Center生成的同一Google帐户的存储桶时,我遇到了问题。 I used the code above by simply changing the bucket name to the value provided by the Google Merchant center from Settings->Google Cloud Storage 我使用了上面的代码,只需将存储桶名称更改为Google Merchant中心从“设置”->“ Google Cloud Storage”提供的值

var bucketToUpload = "merchantxxxxxxx";

This procedure did not get the file uploaded to the bucket (tested with gsutil - bucket exists in my project but is empty). 此过程未将文件上传到存储桶(已通过gsutil测试-存储桶在我的项目中存在,但为空)。 Why can't I upload the file? 为什么我不能上传文件?

Your code does not authorize the access to Google Cloud Storage. 您的代码未授权访问Google云端存储。 After you create ServiceAccountCredential, you must get Access Token by calling: 创建ServiceAccountCredential后,必须通过调用以下方法获取访问令牌:

 ServiceAccountCredential sac = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                Scopes = scopes
            }.FromCertificate(certificate));
 var cts = new CancellationToken();
 var response = sac.RequestAccessTokenAsync(cts).Result;

If the response is true , your ServiceAccountCredential object will have ServiceAccountCredential.Token.AccessToken set. 如果响应为true ,则您的ServiceAccountCredential对象将设置ServiceAccountCredential.Token.AccessToken

Then, when you create your uploadRequest, you must provide the AccessToken you obtained: 然后,当您创建uploadRequest时,您必须提供获得的AccessToken:

var uploadRequest = new ObjectsResource.InsertMediaUpload(service, newObject,
       bucketToUpload, fileStream, "text/plain");

uploadRequest.OauthToken = sac.Token.AccessToken;

And finally call: 最后调用:

uploadRequest.Upload();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM