Exemplo n.º 1
0
        public List <Google.Apis.Storage.v1.Data.Object> GetFoldersAndBlobs(string bucketName, string prefix, string delimiter)
        {
            List <string> folderNames = new List <string>()
            {
            };
            List <Google.Apis.Storage.v1.Data.Object> blobs = new List <Google.Apis.Storage.v1.Data.Object>()
            {
            };

            Google.Apis.Storage.v1.Data.Objects response = GetBlobsRaw(bucketName, prefix, delimiter);
            if (response.Prefixes != null)
            {
                folderNames = response.Prefixes.ToList();
                folderNames.ForEach(fn =>
                {
                    Google.Apis.Storage.v1.Data.Object folderObj = GetBlob(bucketName, fn);
                    if (null != folderObj)
                    {
                        blobs.Add(folderObj);
                    }
                });
            }
            if (response.Items != null)
            {
                blobs.AddRange(response.Items.ToList());
            }
            return(blobs);
        }
Exemplo n.º 2
0
        public Google.Apis.Storage.v1.Data.Objects GetBlobsRaw(string bucketName, string prefix, string delimiter)
        {
            /*
             * Gcs folder behavior, by wesly , 20200501
             * folder name look likes aaa/ and the size is zero, and then if we put a ojbect in the folder, the folder name aaa/
             * will be delete by system automatically, so you can get the object instnce of aaa/ any more.
             * if list the bucket with delimiter, we can get all the direct child objects and folders names(as prefixes)
             * and only these folders which contains no items can get the object instance.
             * if list the bucket with delimiter and IncludeTrailingDelimiter, objects that end in exactly one
             * instance of delimiter will have their metadata included in items in addition to prefixes.
             * but pls pay attention to the duplicated item both in items and prefixes
             */
            StorageService storageService = this.storage.Service;

            ObjectsResource.ListRequest request = storageService.Objects.List(bucketName);
            if (null != delimiter)
            {
                request.Delimiter = delimiter;
            }
            if (null != prefix)
            {
                request.Prefix = prefix;
            }
            //request.IncludeTrailingDelimiter = true;
            Google.Apis.Storage.v1.Data.Objects response = request.Execute();
            return(response);
        }
Exemplo n.º 3
0
        public List <string> GetFolderAndBlobNames(string bucketName, string prefix)
        {
            List <string> rawNames = new List <string>()
            {
            };
            List <string> folderAndBlobs = new List <string>()
            {
            };
            List <Google.Apis.Storage.v1.Data.Object> blobs = null;
            List <string> folders = null;

            Google.Apis.Storage.v1.Data.Objects response = GetBlobsRaw(bucketName, prefix, "/");
            if (response.Prefixes != null)
            {
                folders = response.Prefixes.ToList();
                rawNames.AddRange(folders);
            }
            if (response.Items != null)
            {
                blobs = response.Items.ToList();
                blobs.ForEach(b => { rawNames.Add(b.Name); });
            }
            rawNames.ForEach(b => {
                if (!b.Equals(prefix))
                {
                    string bName = (null == prefix) ? b : Utils.TrimPrefix(b, prefix);
                    folderAndBlobs.Add(bName);
                }
            });
            return(folderAndBlobs);
        }
        protected override async Task <IReadOnlyCollection <Blob> > ListAtAsync(string path, ListOptions options, CancellationToken cancellationToken)
        {
            ObjectsResource.ListRequest request = _client.Service.Objects.List(_bucketName);
            request.Prefix    = StoragePath.IsRootPath(path) ? null : (path + "/");
            request.Delimiter = "/";

            var page = new List <Blob>();

            do
            {
                Objects serviceObjects = await request.ExecuteAsync().ConfigureAwait(false);

                if (serviceObjects.Items != null)
                {
                    page.AddRange(GConvert.ToBlobs(serviceObjects.Items, options));
                }

                if (serviceObjects.Prefixes != null)
                {
                    //the only info we have about prefixes is it's name
                    page.AddRange(serviceObjects.Prefixes.Select(p => new Blob(p, BlobItemKind.Folder)));
                }


                request.PageToken = serviceObjects.NextPageToken;
            }while(request.PageToken != null);

            return(page);
        }