Google云端存储:从hiarchial /子文件夹结构中获取对象

时间:2018-02-06 13:25:07

标签: c# api google-cloud-platform cloud storage

我已经创建了用于管理文件的层次结构。计划为每个月创建文件夹,即2017年12月。每个文件夹中将有超过10k个pdf文件。

用于从存储桶中获取对象的编写C#代码。此代码适用于访问存储桶根目录下的文件。我在访问文件夹中的文件时遇到问题,即不使用代码访问my-bucket / dec-2017 / test.pdf。

Refer my bucket structure here

我正在使用以下代码,之前有人可以这样做吗?

if (_storageService == null)
{
    _storageService = CreateStorageClient();
}
ObjectsResource.GetRequest downloadRequest = null;
//string objectName = "dec-2017%2Ftest.pdf";
string objectName = "dec-2017/test.pdf";

downloadRequest = new ObjectsResource.GetRequest(_storageService, bucketName, objectName);

MemoryStream stream = new MemoryStream();
downloadRequest.Download(stream);
bytes = stream.ToArray();

1 个答案:

答案 0 :(得分:0)

请检查下面的示例代码-

        using Google.Apis.Auth.OAuth2;
        using Google.Cloud.Storage.V1;

        string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Test.csv");
        File.WriteAllText(file, "test");

        GoogleCredential credential = null;
        BucketConnector bucketConnector = new BucketConnector();
        credential = bucketConnector.ConnectStream();
        var storageClient = StorageClient.Create(credential);

        string folderPath = ConfigurationParameters.FOLDER_NAME_IN_BUCKET;
        using (FileStream file = File.OpenRead(localPath))
        {
            objectName = folderPath + Path.GetFileName(localPath);
            storage.UploadObject(bucketName, objectName, null, file);           
        }