public string PutObject(string bucketName, string key, Stream fileStream)
 {
     try
     {
         ObsClient        client  = new ObsClient(_config.Pk, _config.Secret, _config.Endpoint);
         PutObjectRequest request = new PutObjectRequest()
         {
             BucketName  = bucketName,
             ObjectKey   = key,
             InputStream = fileStream,
         };
         var res = client.PutObject(request);
         if (res.StatusCode == HttpStatusCode.OK)
         {
             if (_config.IsUsedOriginalUrl)
             {
                 return(res.ObjectUrl);//使用华为云原始链接
             }
             return($"{_config.BucketDomainScheme}://{_config.BucketDomains[bucketName]}/{key}");
         }
         return(null);
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
        public static void Main(string[] args)
        {
            // Constructs a obs client instance with your account for accessing OBS
            ObsConfig config = new ObsConfig();

            config.Endpoint = endpoint;
            client          = new ObsClient(AK, SK, config);

            // Create bucket
            DoCreateBucket();

            // Set/Get/Delete bucket cors
            DoBucketCorsOperations();

            // Create object
            DoCreateObject();

            // Get object
            DoGetObject();

            // Set/Get object acl
            DoObjectAclOperations();

            // Delete object
            DoDeleteObject();

            // Delete bucket
            //DoDeleteBucket();


            Console.ReadKey();
        }
예제 #3
0
        private void CreateObsClient()
        {
            try
            {
                //初始化配置参数
                ObsConfig config = new ObsConfig();
                config.Endpoint = _endpoint;
                config.AuthType = AuthTypeEnum.OBS;
                // 创建ObsClient实例
                _obsClient = new ObsClient(_accessKey, _secretKey, config);
                // 使用访问OBS

                HeadBucketRequest request = new HeadBucketRequest
                {
                    BucketName = _bucketName,
                };
                _obsClient.HeadBucket(request);
                //if (_obsClient.HeadBucket(request) == false)
                //{
                //    CreateBucketRequest requestCreate = new CreateBucketRequest();
                //    requestCreate.BucketName = _bucketName;
                //    _obsClient.CreateBucket(requestCreate);
                //}
            }
            catch (ObsException ex)
            {
                throw new Exception("上传Obs错误:" + ex.ErrorCode + ":" + ex.Message);
            }
        }
예제 #4
0
 public StorageObs(string name, StorageObsOptions options, ILogger <StorageObs> logger) : base(name, options, logger)
 {
     if (Logger.IsEnabled(LogLevel.Trace))
     {
         Logger.LogTrace($"constructor()");
     }
     _options = options;
     _obs     = new ObsClient(options.AccessKeyId, options.SecretAccessKey, options.Server);
 }
        public static void Main(string[] args)
        {
            client = new ObsClient(AK, SK, endpoint);

            //put object
            PutObject();

            //get object
            GetObject();

            //copy object
            CopyObject();

            //get object metadata
            GetObjectMetadata();

            //set object acl
            SetObjectACL();

            //get obeject acl
            GetObjectACL();

            //append object
            AppendObject();

            //restore object
            RestoreObject();

            //initiate multipart upload
            InitiateMultipartUpload();

            //abort multipart upload
            //AbortMultipartUpload();

            //upload part
            UploadPart();

            //copy part
            //CopyPart();

            //list parts
            ListParts();

            //complete multipart upload
            CompleteMultipartUpload();

            //delete object
            DeleteObject();

            //delete objects
            DeleteObjects();

            Console.WriteLine("object opration end...\n");
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
예제 #6
0
        /// <summary>
        /// 上传指定文件到华为云BOS。
        /// </summary>
        /// <param name="file">待上传的文件</param>
        /// <param name="suffix">文件在云空间里的后缀</param>
        /// <exception cref="Exception"/>
        public Task Upload(string file, string suffix)
        {
            var client = new ObsClient(accessKeyId, secretAccessKey, endpoint);
            PutObjectRequest request = new PutObjectRequest()
            {
                BucketName = bucket,
                ObjectKey  = path + suffix,
                FilePath   = file,
                Expires    = deleteAfterDays
            };

            client.PutObject(request);
            return(Task.CompletedTask);
        }
예제 #7
0
        public HaweiOSSService(IMemoryCache cache, OSSOptions options) : base(cache, options)
        {
            string endPoint = options.Endpoint;

            //如果是不带协议的endpoint,添加协议
            if (!endPoint.StartsWith("http", StringComparison.OrdinalIgnoreCase))
            {
                endPoint = options.IsEnableHttps ? "https://" + endPoint : "http://" + endPoint;
            }
            _client = new ObsClient(Options.AccessKey, Options.SecretKey, new ObsConfig()
            {
                Endpoint = endPoint
            });
        }
 public bool DeleteObject(string bucketName, string key)
 {
     try
     {
         ObsClient           client  = new ObsClient(_config.Pk, _config.Secret, _config.Endpoint);
         DeleteObjectRequest request = new DeleteObjectRequest()
         {
             BucketName = bucketName,
             ObjectKey  = key
         };
         var res = client.DeleteObject(request);
         return(res.StatusCode == HttpStatusCode.OK || res.StatusCode == HttpStatusCode.NoContent);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
        /// <summary>
        /// 序列化记录文件的数据一致性校验
        /// Md5值;临时文件的名字、大小;对象的大小、最后修改时间、Etag值
        /// </summary>
        /// <param name="tmpFilePath"></param>
        /// <param name="obsClient"></param>
        /// <returns></returns>
        public bool IsValid(string tmpFilePath, ObsClient obsClient)
        {
            if (this.Md5 != ComputeHash.HashCode <DownloadCheckPoint>(this))
            {
                return(false);
            }

            FileInfo fileInfo = new FileInfo(tmpFilePath);

            if (!this.TmpFileStatus.TmpFilePath.Equals(tmpFilePath) || this.TmpFileStatus.Size != fileInfo.Length)
            {
                return(false);
            }

            GetObjectMetadataResponse response = obsClient.GetObjectMetadata(BucketName, ObjectKey, VersionId);

            if (!this.ObjectStatus.Etag.Equals(response.ETag) || this.ObjectStatus.Size != response.ContentLength || this.ObjectStatus.LastModified != response.LastModified)
            {
                return(false);
            }

            return(true);
        }
예제 #10
0
 static HuaWeiImageHelper()
 {
     client = new ObsClient(AK, SK, endpoint);
 }
예제 #11
0
        private void btnCDC_Click(object sender, RoutedEventArgs e)
        {
            obs = new ObsClient(tbIP.Text, tbPort.Text, tbPassword.Text);

            obs.connect();
        }
예제 #12
0
        public static void Main(string[] args)
        {
            client = new ObsClient(AK, SK, endpoint);

            //create bucket
            CreateBucket();

            Console.ReadKey();

            //head bucket
            HeadBucket();

            //list buckets
            ListBuckets();

            //list objects
            ListObjects();

            //list objects(multi-version)
            ListVersions();

            //get bucket metadata
            GetBucketMetadata();

            //get bucket location
            GetBucketLocation();

            //get bucket  storageinfo
            GetBucketStorageInfo();

            //set bucket quota
            SetBucketQuota();

            //get bucket quota
            GetBucketQuota();

            //set bucket acl
            SetBucketACL();

            //get buclet acl
            GetBucketACL();

            //set bucket lifecycle
            SetBucketLifecycle();

            //get bucket lifecycle
            GetBucketLifecycle();

            //delete bucket lifecycle
            DeleteBucketLifecycle();

            //set bucket website
            SetBucketWebsite();

            //get bucket website
            GetBucketWebsite();

            //delete bucket website
            DeleteBucketWebsite();

            //set bucket version status
            SetBucketVersioning();

            //get bucket version status
            GetBucketVersioning();

            //set bucket cors
            SetBucketCors();

            //get bucket cors
            GetBucketCors();

            //delete bucket cors
            DeleteBucketCors();

            //set bucket logging
            SetBucketLogging();

            //get bucket logging
            GetBucketLogging();

            //delete bucket logging
            DeleteBucketLogging();

            //set bucket tagging
            SetBucketTagging();

            //get bucket tagging
            GetBucketTagging();

            //delete bucket tagging
            DeleteBucketTagging();

            //set bucket nontification
            SetBucketNotification();

            //get bucket nontification
            GetBucketNotification();

            //delete bucket nontification
            DeleteBucketNotification();

            //list multipart uploads
            ListMultipartUploads();

            //set bucket storage policy
            SetBucketStoragePolicy();

            //get bucket storage policy
            GetBucketStoragePolicy();

            //set bucket policy
            SetBucketPolicy();

            //get bucket policy
            GetBucketPolicy();

            //delete bucket policy
            DeleteBucketPolicy();


            //delete bucket
            DeleteBucket();

            Console.WriteLine("bucket operation end...\n\n");
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }