zoukankan      html  css  js  c++  java
  • AWSS3异步等待上传成功返回结果

            /// <summary>
            /// 流上传文件
            /// </summary>
            /// <param name="data">流内容</param>
            /// <param name="key">文件名(标识)</param> 
            /// <returns></returns>
            public async Task<bool> UploadFile(byte[] data,string contentType,  string key)
            {
                try
                {  
                    Stream s = (Stream)new MemoryStream(data);
                    PutObjectRequest request = new PutObjectRequest();
                    request.BucketName = AwsOptions.OpenBucket;
                    request.Key = key;
                    request.ContentType = contentType;               
                    request.InputStream = s;
    
                    using (var ss =  S3Client.PutObjectAsync(request))
                    {
                        //等待结果返回
                        while (ss.IsCompleted == false)
                        {
                            Console.Write(".");
                            //每隔2秒检查一次
                            System.Threading.Thread.Sleep(1000);
                        }
                        //返回上传是否成功
                        return ss.IsCompletedSuccessfully;
                    }
                }
                catch (AmazonS3Exception e)
                {
                    //Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
                    throw new Exception(e.Message);
                }
                catch (Exception e)
                {
                    throw new Exception($"图片上传错误:{e.Message + e.StackTrace}");
                }
                return true;
            }
    

      

  • 相关阅读:
    网页源码爬取
    Vue模板语法上集
    SSH整合
    P1290 欧几里德的游戏
    P1582 倒水
    P2158 [SDOI2008]仪仗队
    P1338 末日的传说
    P1372 又是毕业季I
    P1865 A % B Problem
    P1031 均分纸牌
  • 原文地址:https://www.cnblogs.com/chongerwangzi/p/10100834.html
Copyright © 2011-2022 走看看