/// <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; }