Ejemplo n.º 1
0
        public static void SyncGetObject(string bucketName, string key)
        {
            OssClient ossClient = new OssClient(Config.Endpoint, Config.AccessKeyId, Config.AccessKeySecret);

            try
            {
                var result = ossClient.GetObject(bucketName, key);

                using (var requestStream = result.Content)
                {
                    using (var fs = File.Open(Config.DirToDownload + "/" + key, FileMode.OpenOrCreate))
                    {
                        int length = 4 * 1024;
                        var buf    = new byte[length];
                        do
                        {
                            length = requestStream.Read(buf, 0, length);
                            fs.Write(buf, 0, length);
                        }while (length != 0);
                    }
                }

                Debug.LogError("Get object succeeded");
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// oss上传
        /// </summary>
        /// <param name="prepareInfo">酷安接口返回的信息</param>
        /// <param name="fileInfo">酷安接口返回的文件信息</param>
        /// <param name="stream">文件流</param>
        /// <param name="contentType">上传文件的imei类型</param>
        /// <returns>响应数据</returns>
        /// <exception cref="OssException">OSS异常</exception>
        /// <exception cref="Newtonsoft.Json.JsonSerializationException">JSON序列化异常</exception>
        public static Resp <OssUploadPictureResponse> OssUpload(
            OssUploadPicturePrepareResultUploadPrepareInfo prepareInfo,
            OssUploadPicturePrepareResultFileInfo fileInfo,
            Stream stream,
            string contentType,
            OssUploadProgressHandler progressHandler = null)
        {
            var oss = new Aliyun.OSS.OssClient(
                prepareInfo.EndPoint.Replace("https://", ""),
                prepareInfo.AccessKeyId,
                prepareInfo.AccessKeySecret,
                prepareInfo.SecurityToken
                );
            var callback = "eyJjYWxsYmFja0JvZHlUeXBlIjoiYXBwbGljYXRpb25cL2pzb24iLCJjYWxsYmFj" +
                           "a0hvc3QiOiJhcGkuY29vbGFway5jb20iLCJjYWxsYmFja1VybCI6Imh0dHBzOlwvXC9hcGkuY29vbGF" +
                           "way5jb21cL3Y2XC9jYWxsYmFja1wvbW9iaWxlT3NzVXBsb2FkU3VjY2Vzc0NhbGxiYWNrP2NoZWNrQX" +
                           "J0aWNsZUNvdmVyUmVzb2x1dGlvbj0wJnZlcnNpb25Db2RlPTIxMDIwMzEiLCJjYWxsYmFja0JvZHkiO" +
                           "iJ7XCJidWNrZXRcIjoke2J1Y2tldH0sXCJvYmplY3RcIjoke29iamVjdH0sXCJoYXNQcm9jZXNzXCI6" +
                           "JHt4OnZhcjF9fSJ9";
            var callbackVar = "eyJ4OnZhcjEiOiJmYWxzZSJ9";
            var metadata    = new ObjectMetadata {
                ContentMd5  = OssUtils.ComputeContentMd5(stream, stream.Length),
                ContentType = contentType
            };

            metadata.AddHeader(HttpHeaders.Callback, callback);
            metadata.AddHeader(HttpHeaders.CallbackVar, callbackVar);

            var request = new PutObjectRequest(
                prepareInfo.Bucket,
                fileInfo.UploadFileName,
                stream,
                metadata);

            request.StreamTransferProgress += (object sender, StreamTransferProgressArgs args) => {
                // 文件上传进度回调
                progressHandler?.Invoke(fileInfo, sender, args);
            };
            var putResult = oss.PutObject(request);

            // 相应数据
            var response = GetCallbackResponse(putResult);
            var jsonObj  = JsonConvert.DeserializeObject <Resp <OssUploadPictureResponse> >(response);

            return(jsonObj);
        }
Ejemplo n.º 3
0
        public static void AsyncGetObject(string bucketName, string key)
        {
            OssClient ossClient = new OssClient(Config.Endpoint, Config.AccessKeyId, Config.AccessKeySecret);

            try
            {
                ossClient.BeginGetObject(bucketName, key, GetObjectCallback, key.Clone());

                // _event.WaitOne();
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
Ejemplo n.º 4
0
        public static void GetObjectByRequest(string bucketName, string key)
        {
            OssClient ossClient = new OssClient(Config.Endpoint, Config.AccessKeyId, Config.AccessKeySecret);

            try
            {
                var request = new GetObjectRequest(bucketName, key);
                request.SetRange(0, 100);

                var result = ossClient.GetObject(request);

                UnityEngine.Debug.LogError("Get object succeeded, length:{0}" + result.Metadata.ContentLength);
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
Ejemplo n.º 5
0
        private static void GetObjectCallback(IAsyncResult ar)
        {
            OssClient ossClient = new OssClient(Config.Endpoint, Config.AccessKeyId, Config.AccessKeySecret);

            try
            {
                var result = ossClient.EndGetObject(ar);
                Debug.LogError("total length:" + result.Metadata.ContentLength);
                int downloadLen = 0;
                using (var requestStream = result.Content)
                {
                    using (var fs = File.Open(Config.DirToDownload + "/" + (ar.AsyncState as string), FileMode.OpenOrCreate))
                    {
                        int length = 4 * 1024;
                        var buf    = new byte[length];
                        do
                        {
                            length = requestStream.Read(buf, 0, length);
                            fs.Write(buf, 0, length);
                            downloadLen       += length;
                            m_DownLoadProgress = (float)downloadLen / result.Metadata.ContentLength;
                            m_DownLoadState    = DownLoadState.DOWNLOADING;
                        } while (length != 0);
                    }
                }
                m_DownLoadState = DownLoadState.DOWNLOADCOMPLETE;
                Debug.LogError("download done!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                _event.Set();
            }
        }
 public ResumableUploadManager(OssClient ossClient, int maxRetryTimes, ClientConfiguration conf)
 {
     this._ossClient     = ossClient;
     this._maxRetryTimes = maxRetryTimes;
     this._conf          = conf;
 }
Ejemplo n.º 7
0
 public OssApiHelper()
 {
     _client     = new OssClient(Config.GetString("OssEndpoint"), Config.GetString("OssAccessKeyId"), Config.GetString("OssAccessKeySecret"));
     _bucketName = Config.GetString("OssBucketName");
 }