This class maintains a local description of the objects in a bucket. It is used by the GoogleCloudStorageProvider to prevent redundant service calls e.g. to discover if an object exists. It keeps track of real objects, which we treat as files, and of name prefixes, which act like folders. A real object named "myFolder/" will be both a prefix "myFolder" and an object "myFolder/".
Esempio n. 1
0
        /// <summary>
        /// PowerShell uses this to check if items exist.
        /// </summary>
        protected override bool ItemExists(string path)
        {
            var gcsPath = GcsPath.Parse(path);

            switch (gcsPath.Type)
            {
            case GcsPath.GcsPathType.Drive:
                return(true);

            case GcsPath.GcsPathType.Bucket:
                var bucketCache = BucketCache.Value;
                if (bucketCache.ContainsKey(gcsPath.Bucket))
                {
                    return(true);
                }
                try
                {
                    var bucket = Service.Buckets.Get(gcsPath.Bucket).Execute();
                    bucketCache[bucket.Name] = bucket;
                    return(true);
                }
                catch
                {
                    return(false);
                }

            case GcsPath.GcsPathType.Object:
                BucketModel model        = GetBucketModel(gcsPath.Bucket);
                bool        objectExists = model.ObjectExists(gcsPath.ObjectPath);
                return(objectExists);

            default:
                throw new InvalidOperationException($"Unknown Path Type {gcsPath.Type}");
            }
        }
        /// <summary>
        /// PowerShell uses this to check if items exist.
        /// </summary>
        protected override bool ItemExists(string path)
        {
            var gcsPath = GcsPath.Parse(path);

            switch (gcsPath.Type)
            {
            case GcsPath.GcsPathType.Drive:
                return(true);

            case GcsPath.GcsPathType.Bucket:
                Dictionary <string, Bucket> bucketDict = null;
                // If the bucket cache is not initialized, then don't bother initializing it
                // because that will cause a long wait time and we may not even know whether
                // the user needs to use all the other buckets right away. Also, we should not
                // refresh the whole cache right at this instance (which is why we call
                // GetValueWithoutUpdate) for the same reason.
                bucketDict = BucketCache.GetLastValueWithoutUpdate();
                if (bucketDict != null && bucketDict.ContainsKey(gcsPath.Bucket))
                {
                    return(true);
                }

                try
                {
                    var bucket = Service.Buckets.Get(gcsPath.Bucket).Execute();
                    if (bucketDict != null)
                    {
                        bucketDict[bucket.Name] = bucket;
                    }
                    return(true);
                }
                catch
                {
                    return(false);
                }

            case GcsPath.GcsPathType.Object:
                BucketModel model        = GetBucketModel(gcsPath.Bucket);
                bool        objectExists = model.ObjectExists(gcsPath.ObjectPath);
                return(objectExists);

            default:
                throw new InvalidOperationException($"Unknown Path Type {gcsPath.Type}");
            }
        }