【发布时间】:2018-01-18 21:15:30
【问题描述】:
我们有一个小程序,通过对象存储的 Rest API 将一些文件上传到 Softlayer。
我们每天上传大约 38 个文件,总大小为 1.32 GB。文件大小大约在 650KB 到 600MB 之间。
其中一个文件的大小约为 582MB,我们的程序每天都会尝试上传 3 次,但始终不成功。每次尝试上传此文件大约需要 30~45 分钟。
API返回的消息是:
The request was aborted: The request was canceled.
这是我上传文件的代码:
// Lists the Backup files of the folder
DirectoryInfo dirInfoBkps = new DirectoryInfo(backupFolder);
FileInfo[] arrFiles = dirInfoBkps.GetFiles(backupExtension);
// Performs the authentication in Softlayer, and obtains the Token and URL of Upload
RestHelper softlayerRestAPI = new RestHelper();
softlayerRestAPI.RestHeaders.Add("X-Auth-User", apiSoftlayerUser);
softlayerRestAPI.RestHeaders.Add("X-Auth-Key", apiSoftlayerToken);
softlayerRestAPI.RestHeaders.Add("X-Account-Meta-Temp-Url-Key", apiSoftlayerMetaTempUrlKey);
Dictionary<string, string> dicRespondeHeaders;
SoftlayerModel softlayerModel =
softlayerRestAPI.CallGetRestMethod<SoftlayerModel>(apiSoftlayerUrl, out dicRespondeHeaders);
// Prepares to Upload Files
apiSoftlayerUrl = softlayerModel.storage.@public;
apiSoftlayerUrl = apiSoftlayerUrl.Replace("https", "http");
apiSoftlayerXAuthToken = dicRespondeHeaders["X-Storage-Token"];
// Upload each file in the folder
foreach (FileInfo fileInfo in arrFiles)
{
// Creates the Upload URL
string uploadUrl = string.Format("{0}{1}{2}",
apiSoftlayerUrl,
"/Backups_SVN/",
fileInfo.Name);
// Try to make the upload 3-times
int numberOfTries = 0;
Exception lastException = null;
string lastFilename = null;
string mensagem = string.Empty;
while (numberOfTries < 3)
{
try
{
numberOfTries++;
softlayerRestAPI.RestHeaders.Clear();
softlayerRestAPI.RestHeaders.Add("X-Auth-Token", apiSoftlayerXAuthToken);
byte[] arr =
softlayerRestAPI.CallUploadRestMethod(uploadUrl, fileInfo.FullName);
// Upload Successful
break;
}
catch (Exception ex)
{
// Upload failed
lastException = ex;
lastFilename = fileInfo.Name;
Console.WriteLine(ex.Message);
}
}
if (numberOfTries == 3) // All attempts failed
{
// Writes the error log for future reference
}
}
更新
我忘记了RestHelper 类的代码:https://pastebin.com/hBYjXXJh
【问题讨论】:
-
您在使用控制门户(UI)提交问题时是否得到相同的结果?这可能是 softlayer 的问题,而不是您的代码
-
@NelsonRaulCaberoMendoza,我在 Softlayer 的支持上开了一张票,他们说问题出在我的应用程序中,而不是在他们的 API 中……他们要求我发布我的在这里怀疑,在 StackOverflow 中...... ¬¬
-
@MarioCSimãoJr 对不起,Mario 我不是 C# 方面的专家,我只能向您发送一些文档,例如 sldn.softlayer.com/blog/waelriac/… 或建议您使用 Softlayer 客户端进行对象存储,这些客户端可在此处获得 @987654324 @
-
@NelsonRaulCaberoMendoza,感谢您的关注。我的程序已经实现了您发送的链接中描述的步骤。我也查看了您发送的客户端库,但其中没有一个是用于 .NET 的……但是没关系,现在一切正常……再次感谢您的帮助。
标签: c# file-upload ibm-cloud-infrastructure