Exemple #1
0
        /// <summary>
        /// 获取对象信息
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="objectName"></param>
        /// <param name="versionID"></param>
        /// <param name="matchEtag"></param>
        /// <param name="modifiedSince"></param>
        /// <returns></returns>
        public Task <ItemMeta> GetObjectMetadataAsync(string bucketName, string objectName, string versionID = null, string matchEtag = null, DateTime?modifiedSince = null)
        {
            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException(nameof(bucketName));
            }
            objectName = FormatObjectName(objectName);

            GetObjectMetadataRequest request = new GetObjectMetadataRequest()
            {
                BucketName = bucketName,
                ObjectKey  = objectName,
                VersionId  = versionID,
            };
            GetObjectMetadataResponse response = _client.GetObjectMetadata(request);
            var newMeta = new ItemMeta()
            {
                ObjectName    = objectName,
                ContentType   = response.ContentType,
                Size          = response.ContentLength,
                LastModified  = response.LastModified.GetValueOrDefault(),
                ETag          = response.ETag,
                IsEnableHttps = Options.IsEnableHttps,
                MetaData      = new Dictionary <string, string>(),
            };

            if (response.Metadata != null && response.Metadata.Count > 0)
            {
                foreach (var item in response.Metadata.KeyValuePairs)
                {
                    newMeta.MetaData.Add(item.Key, item.Value);
                }
            }
            return(Task.FromResult(newMeta));
        }
        public async Task <ItemMeta> GetObjectMetadataAsync(string bucketName, string objectName, string versionID = null, string matchEtag = null, DateTime?modifiedSince = null)
        {
            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException(nameof(bucketName));
            }
            if (string.IsNullOrEmpty(objectName))
            {
                throw new ArgumentNullException(nameof(objectName));
            }
            bucketName = ConvertBucketName(bucketName);
            HeadObjectRequest request = new HeadObjectRequest(bucketName, objectName);

            if (!string.IsNullOrEmpty(versionID))
            {
                request.SetVersionId(versionID);
            }
            //执行请求
            HeadObjectResult result = _client.HeadObject(request);

            if (!result.IsSuccessful())
            {
                throw new Exception("Query object meta data failed.");
            }
            ItemMeta metaData = new ItemMeta()
            {
                ObjectName    = objectName,
                Size          = result.size,
                ETag          = result.eTag,
                IsEnableHttps = Options.IsEnableHttps,
            };

            return(await Task.FromResult(metaData));
        }
        public async Task <ItemMeta> GetObjectMetadataAsync(string bucketName, string objectName, string versionID = null, string matchEtag = null, DateTime?modifiedSince = null)
        {
            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException(nameof(bucketName));
            }
            if (string.IsNullOrEmpty(objectName))
            {
                throw new ArgumentNullException(nameof(objectName));
            }
            GetObjectMetadataRequest request = new GetObjectMetadataRequest(bucketName, objectName)
            {
                VersionId = versionID
            };
            var oldMeta = _client.GetObjectMetadata(request);
            // 设置新的文件元信息。
            var newMeta = new ItemMeta()
            {
                ObjectName    = objectName,
                ContentType   = oldMeta.ContentType,
                Size          = oldMeta.ContentLength,
                LastModified  = oldMeta.LastModified,
                ETag          = oldMeta.ETag,
                IsEnableHttps = Options.IsEnableHttps,
                MetaData      = new Dictionary <string, string>(),
            };

            if (oldMeta.UserMetadata != null && oldMeta.UserMetadata.Count > 0)
            {
                foreach (var item in oldMeta.UserMetadata)
                {
                    newMeta.MetaData.Add(item.Key, item.Value);
                }
            }
            return(await Task.FromResult(newMeta));
        }