public class AwsS3Helper { // 请求S3的凭据 AWSCredentials awsCredentials = new BasicAWSCredentials(ConfigurationManager.AppSettings["accessKey"] , ConfigurationManager.AppSettings["secretKey"]); // 获取桶的名称 private string bucketName = ConfigurationManager.AppSettings["bucketName"].ToString(); // 获取桶下文件夹名称 private string bucketFileName = ConfigurationManager.AppSettings["bucketFileName"].ToString(); private AmazonS3Client client { get { return new AmazonS3Client(awsCredentials, RegionEndpoint.APNortheast1); } } /// <summary> /// 上传文件 /// </summary> /// <param name="filePath">文件所在地址</param> /// <returns></returns> public string UploadFile(string filePath,string fileName) { try { string bucketPath = GetFilePath(); PutObjectRequest request = new PutObjectRequest { BucketName = bucketPath, Key = fileName, FilePath = filePath }; PutObjectResponse response = client.PutObject(request); return response.HttpStatusCode.ToString(); ; } catch (Exception ex) { throw ex; } } /// <summary> /// 路径处理 /// </summary> /// <param name="folderName"></param> /// <returns></returns> public string GetFilePath() { //// 正式 string filePath = bucketName + "/"+ bucketFileName; return filePath; } }
string FileName = "abc" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt"; string InterfaceFilePath = FilePath.Trim('/') +"/"+ FileName; S3.UploadFile(InterfaceFilePath, FileName);