示例#1
0
        public async Task <OperationResult> SetReadPolicy(string key, ReadAccess access)
        {
            try
            {
                Bucket bucket = await _client.GetBucketAsync(key);

                PredefinedBucketAcl bacl = access == ReadAccess.Public
                    ? PredefinedBucketAcl.PublicRead
                    : PredefinedBucketAcl.Private;
                PredefinedObjectAcl oacl = access == ReadAccess.Public
                    ? PredefinedObjectAcl.PublicRead
                    : PredefinedObjectAcl.Private;
                _client.UpdateBucket(bucket, new UpdateBucketOptions()
                {
                    PredefinedAcl = bacl,
                    PredefinedDefaultObjectAcl = oacl
                });
                var buckets = _client.ListObjects(key).ReadPage(Int32.MaxValue / 2);
                IEnumerable <Task> tasks = buckets.Select(s =>
                                                          _client.UpdateObjectAsync(s, new UpdateObjectOptions()
                {
                    PredefinedAcl = oacl
                }));
                await Task.WhenAll(tasks);

                return(new OperationResult(true, "", HttpStatusCode.OK));
            }
            catch (GoogleApiException e)
            {
                return(new OperationResult(false, e.Message, (HttpStatusCode)e.Error.Code));
            }
        }
        public override IDataStore Configure(string tenant, Handler handlerConfig, Module moduleConfig, IDictionary <string, string> props)
        {
            _tenant = tenant;

            if (moduleConfig != null)
            {
                _modulename = moduleConfig.Name;
                _dataList   = new DataList(moduleConfig);

                _domainsExpires = moduleConfig.Domain.Where(x => x.Expires != TimeSpan.Zero).ToDictionary(x => x.Name, y => y.Expires);

                _domainsExpires.Add(string.Empty, moduleConfig.Expires);

                _domainsAcl = moduleConfig.Domain.ToDictionary(x => x.Name, y => GetGoogleCloudAcl(y.Acl));
                _moduleAcl  = GetGoogleCloudAcl(moduleConfig.Acl);
            }
            else
            {
                _modulename = string.Empty;
                _dataList   = null;

                _domainsExpires = new Dictionary <string, TimeSpan> {
                    { string.Empty, TimeSpan.Zero }
                };
                _domainsAcl = new Dictionary <string, PredefinedObjectAcl>();
                _moduleAcl  = PredefinedObjectAcl.PublicRead;
            }

            _bucket = props["bucket"];

            _bucketRoot = props.ContainsKey("cname") && Uri.IsWellFormedUriString(props["cname"], UriKind.Absolute)
                              ? new Uri(props["cname"], UriKind.Absolute)
                              : new Uri(string.Format("https://storage.googleapis.com/{0}/", _bucket), UriKind.Absolute);

            _bucketSSlRoot = props.ContainsKey("cnamessl") &&
                             Uri.IsWellFormedUriString(props["cnamessl"], UriKind.Absolute)
                                 ? new Uri(props["cnamessl"], UriKind.Absolute)
                                 : new Uri(string.Format("https://storage.googleapis.com/{0}/", _bucket), UriKind.Absolute);

            if (props.ContainsKey("lower"))
            {
                bool.TryParse(props["lower"], out _lowerCasing);
            }

            _json = props["json"];

            if (props.ContainsKey("subdir"))
            {
                _subDir = props["subdir"];
            }

            return(this);
        }
        public GoogleCloudStorage(string tenant)
        {
            _tenant = tenant;

            _modulename = string.Empty;
            _dataList   = null;

            _domainsExpires = new Dictionary <string, TimeSpan> {
                { string.Empty, TimeSpan.Zero }
            };
            _domainsAcl = new Dictionary <string, PredefinedObjectAcl>();
            _moduleAcl  = PredefinedObjectAcl.PublicRead;
        }
示例#4
0
        public GoogleCloudStorage(string tenant, Handler handlerConfig, Module moduleConfig)
        {
            _tenant = tenant;

            _modulename = moduleConfig.Name;
            _dataList   = new DataList(moduleConfig);

            _domainsExpires =
                moduleConfig.Domain.Where(x => x.Expires != TimeSpan.Zero).
                ToDictionary(x => x.Name,
                             y => y.Expires);

            _domainsExpires.Add(string.Empty, moduleConfig.Expires);

            _domainsAcl = moduleConfig.Domain.ToDictionary(x => x.Name, y => GetGoogleCloudAcl(y.Acl));
            _moduleAcl  = GetGoogleCloudAcl(moduleConfig.Acl);
        }
示例#5
0
        public override async Task <string> SaveAsync(FileSetOptions fileSetOptions)
        {
            GoogleCredential credential = await AuthorizedAsync(fileSetOptions);

            StorageClient storageClinet = await StorageClient.CreateAsync(credential);

            PredefinedObjectAcl predefinedObjectAcl = PredefinedObjectAcl.ProjectPrivate;
            PredefinedBucketAcl predefinedBucketAcl = PredefinedBucketAcl.ProjectPrivate;

            switch (fileSetOptions.FileAccess)
            {
            case FileAccessLevel._private:
                predefinedObjectAcl = PredefinedObjectAcl.AuthenticatedRead;
                predefinedBucketAcl = PredefinedBucketAcl.AuthenticatedRead;
                break;

            case FileAccessLevel._public:
                predefinedObjectAcl = PredefinedObjectAcl.PublicRead;
                predefinedBucketAcl = PredefinedBucketAcl.PublicRead;
                break;
            }


            if (fileSetOptions.folderOptions == FolderOptions.CreateIfNull)
            {
                var folder = await storageClinet.GetBucketAsync(fileSetOptions.Folder);

                if (folder == null)
                {
                    await storageClinet.CreateBucketAsync(fileSetOptions.ProjectId, fileSetOptions.Folder, new CreateBucketOptions()
                    {
                        PredefinedAcl = predefinedBucketAcl, PredefinedDefaultObjectAcl = predefinedObjectAcl
                    });
                }
            }

            fileSetOptions._stream.Position = 0;

            await storageClinet.UploadObjectAsync(fileSetOptions.Folder, fileSetOptions.Key, fileSetOptions.ContentType, fileSetOptions._stream, new UploadObjectOptions()
            {
                PredefinedAcl = predefinedObjectAcl
            });

            return(fileSetOptions.Key);
        }