我需要将大尺寸视频上传到Amazon S3存储桶。
我有一个大尺寸的视频
这是我正在使用的班级
public class CustomAmazon
{
public string tourName { get; set; }
public string driveName { get; set; }
public string tourID { get; set; }
public string driveID { get; set; }
public string key { get; set; }
public string bucketName { get; set; }
public string cloudFrontVideoUrl { get; set; }
public string amazonS3VideoUrl { get; set; }
public string filePath { get; set; }
}
这是用于编写对象的亚马逊代码
static string WritingAnObject(CustomAmazon customAmazon)
{
try
{
var videoStream = new FileStream(customAmazon.filePath, FileMode.Open, FileAccess.Read);
// put a more complex object with some metadata and http headers.
string fileName = customAmazon.tourID + "/" + customAmazon.driveID + "/" + Guid.NewGuid() + "__" + Path.GetFileName(customAmazon.filePath);
PutObjectRequest titledRequest = new PutObjectRequest()
{
BucketName = customAmazon.bucketName,
Key = fileName,
InputStream = videoStream,
Timeout = new TimeSpan(4, 30, 30),
ContentType = "video/mov",
CannedACL = S3CannedACL.PublicRead
};
titledRequest.Metadata.Add("title", fileName);
titledRequest.Metadata.Add("drive", customAmazon.driveName);
client.PutObject(titledRequest);
Thread.Sleep(4000);
// Retrieve ACL for object
customAmazon.cloudFrontVideoUrl = customAmazon.cloudFrontVideoUrl + fileName;
customAmazon.key = fileName;
customAmazon.amazonS3VideoUrl = ReadConfig.AWSStorageUrl + fileName;
}
catch (AmazonS3Exception amazonS3Exception)
{
Logger.Write(amazonS3Exception.Message);
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
Console.WriteLine("Please check the provided AWS Credentials.");
Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
}
else
{
Logger.Write(string.Format("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message));
}
}
return customAmazon.amazonS3VideoUrl;
}
上传大尺寸视频时出现错误,而小尺寸视频则正常工作。
答案 0 :(得分:0)
是的,我得到了答案,我使用多播对其进行了尝试,并且工作正常,这是我的解决方案
这是我的课程
public class CustomAmazon
{
public string tourName { get; set; }
public string driveName { get; set; }
public string tourID { get; set; }
public string driveID { get; set; }
public string key { get; set; }
public string bucketName { get; set; }
public string cloudFrontVideoUrl { get; set; }
public string amazonS3VideoUrl { get; set; }
public string filePath { get; set; }
}
这是我的解决方法
在连接凭据后,我只是调用以下方法来上传大视频文件
/// <summary>
/// Method used for DFI (website or API) users to upload a file on Amazon S3 storage
/// </summary>
/// <param name="customAmazon"></param>
/// <returns>CustomAmazon class object </returns>
public CustomAmazon UploadFiletoAmazonS3Storage(CustomAmazon customAmazon)
{
using (client = new AmazonS3Client(ReadConfig.AWSAccessKeyId, ReadConfig.AWSSecretKey, RegionEndpoint.USEast1))
{
Console.WriteLine("Writing an object");
WritingAnLargeObject(customAmazon);
}
return customAmazon;
}
public static string WritingAnLargeObject(CustomAmazon customAmazon)
{
// List to store upload part responses.
List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>();
string fileName = string.Empty;
// Giving custom filename to original file
if (!string.IsNullOrEmpty(customAmazon.tourID) && (!string.IsNullOrEmpty(customAmazon.driveID)))
{
fileName = customAmazon.tourID + "/" + customAmazon.driveID + "/" + Guid.NewGuid() + "___" + Regex.Replace(Path.GetFileName(customAmazon.filePath), @"\s", "");
}
else
{
fileName = Guid.NewGuid() + "___" + Regex.Replace(Path.GetFileName(customAmazon.filePath), @"\s", "");
}
// 1. Initialize MultipartUploadRequest.
InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest
{
BucketName = customAmazon.bucketName,
Key = fileName,
ContentType = "video/mov",
CannedACL = S3CannedACL.PublicRead
};
InitiateMultipartUploadResponse initResponse =
client.InitiateMultipartUpload(initiateRequest);
// 2. Upload video in small Parts of 5 MB.
long contentLength = new FileInfo(customAmazon.filePath).Length;
long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB
try
{
long filePosition = 0;
for (int index = 1; filePosition < contentLength; index++)
{
UploadPartRequest uploadRequest = new UploadPartRequest
{
BucketName = customAmazon.bucketName,
Key = fileName,
UploadId = initResponse.UploadId,
PartNumber = index,
PartSize = partSize,
FilePosition = filePosition,
FilePath = customAmazon.filePath
};
// Upload part and add response to our list.
uploadResponses.Add(client.UploadPart(uploadRequest));
filePosition += partSize;
}
// Step 3: complete.
CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest
{
BucketName = customAmazon.bucketName,
Key = fileName,
UploadId = initResponse.UploadId,
//PartETags = new List<PartETag>(uploadResponses)
};
completeRequest.AddPartETags(uploadResponses);
customAmazon.key = fileName;
CompleteMultipartUploadResponse completeUploadResponse = client.CompleteMultipartUpload(completeRequest);
customAmazon.cloudFrontVideoUrl = customAmazon.cloudFrontVideoUrl + fileName;
customAmazon.amazonS3VideoUrl = ReadConfig.AWSStorageUrl + fileName;
}
catch (Exception exception)
{
Logger.Write(exception.Message);
Console.WriteLine("Exception occurred: {0}", exception.Message);
AbortMultipartUploadRequest abortMPURequest = new AbortMultipartUploadRequest
{
BucketName = customAmazon.bucketName,
Key = fileName,
UploadId = initResponse.UploadId
};
client.AbortMultipartUpload(abortMPURequest);
}
return fileName;
}
最后我将视频上传到S3存储桶