可以使用分片上传来绕过此问题。以下是使用C#的示例代码:
var chunkSize = 5 * 1024 * 1024; // 5MB
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
var sessionId = "";
// Step 1: Initialize the upload session
var initSessionReq = new RestRequest(Method.POST);
initSessionReq.Resource = "/oss/v2/buckets/{bucketKey}/objects/{objectKey}/resumable";
initSessionReq.AddParameter("bucketKey", bucketKey, ParameterType.UrlSegment);
initSessionReq.AddParameter("objectKey", objectKey, ParameterType.UrlSegment);
initSessionReq.AddHeader("Content-Type", "application/json");
initSessionReq.AddHeader("Content-Length", fileStream.Length.ToString());
initSessionReq.AddHeader("x-upload-content-type", "application/octet-stream");
initSessionReq.AddHeader("x-upload-content-length", fileStream.Length.ToString());
var initSessionRes = client.Execute(initSessionReq);
if (initSessionRes.StatusCode == HttpStatusCode.Created)
{
sessionId = JsonConvert.DeserializeObject(initSessionRes.Content).sessionId.Value;
}
else
{
throw new Exception("Failed to initiate resumable upload session");
}
// Step 2: Upload the file chunks
var chunkCount = (int)Math.Ceiling((double)fileStream.Length / chunkSize);
for (int i = 0; i < chunkCount; i++)
{
var start = i * chunkSize;
var end = Math.Min(start + chunkSize, fileStream.Length);
var fileChunk = new byte[chunkSize];
fileStream.Read(fileChunk, 0, fileChunk.Length);
var uploadReq = new RestRequest(Method.PUT);
uploadReq.Resource = "/oss/v2/buckets/{bucketKey}/objects/{objectKey}/resumable/{sessionId}";
uploadReq.AddParameter("bucketKey", bucketKey, ParameterType.UrlSegment);
uploadReq.AddParameter("objectKey", objectKey, ParameterType.UrlSegment);
uploadReq.AddParameter("sessionId", sessionId, ParameterType.UrlSegment);
uploadReq.AddHeader("Content-Length", (end - start).ToString());
uploadReq.AddHeader("Content-Range", $"{start}-{end - 1}/{file