gad*_*ver 0 azure xamarin.ios azure-storage-blobs xamarin nsurlsession
问题陈述:我们需要将日志数据从 Xamarin.IOS 应用程序上传到 Azure 存储。日志不是由应用程序的用户创建的,并且在生成日志后,用户在任何时间内保持应用程序打开没有任何限制。我们希望可靠地上传我们的日志,并牢记以下几点:
在查看此问题的潜在解决方案时,Xamarin 文档指出在 iOS7+ 中:
NSURLSession 允许我们创建任务来:
- 通过网络和设备中断传输内容。
- 上传和下载大文件(后台传输服务)。
所以看起来 NSURLSession 是这类工作的一个很好的候选者,但我想知道我是否在重新发明轮子。WindowsAzure.Storage客户端库是否尊重应用后台基于NSURLSession的上传实现,或者如果我想在后台上传数据,是否需要上传到我用POST方法控制的中间服务器,然后中继数据到 Azure 存储?公共 Azure 文档中似乎没有任何迹象表明可以通过计划任务完成上传。
我得到了这个工作。我已将类和方法简化为一个方法。这里只有必需品。
public void UploadFile(File playbackFile)
{
/// Specify your credentials
var sasURL = "?<the sastoken>";
/// Azure blob storage URL
var storageAccount = "https://<yourstorageaccount>.blob.core.windows.net/<your container name>";
/// specify a UNIQUE session name
var configuration =
NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration("A background session name");
/// create the session with a delegate to recieve callbacks and debug
var session = NSUrlSession.FromConfiguration(
configuration,
new YourSessionDelegate(),
new NSOperationQueue());
/// Construct the blob endpoint
var url = $"{storageAccount}/{playbackFile.Name}{sasURL}";
var uploadUrl = NSUrl.FromString(url);
/// Add any headers for Blob PUT. x-ms-blob-type is REQUIRED
var dic = new NSMutableDictionary();
dic.Add(new NSString("x-ms-blob-type"), new NSString("BlockBlob"));
/// Create the request with NSMutableUrlRequest
/// A default NSUrlRequest.FromURL() is immutable with a GET method
var request = new NSMutableUrlRequest(uploadUrl);
request.Headers = dic;
request.HttpMethod = "PUT";
/// Create the task
var uploadTask = session.CreateUploadTask(
request,
NSUrl.FromFilename(playbackFile.FullName));
/// Start the task
uploadTask.Resume();
}
/// Delegate to recieve callbacks. Implementations are omitted for brevity
public class YourSessionDelegate: NSUrlSessionDataDelegate
{
public override void DidBecomeInvalid(NSUrlSession session, NSError error)
{
Console.WriteLine(error.Description);
}
public override void DidSendBodyData(NSUrlSession session, NSUrlSessionTask task, long bytesSent, long totalBytesSent, long totalBytesExpectedToSend)
{
Console.WriteLine(bytesSent);
}
public override void DidReceiveData(NSUrlSession session, NSUrlSessionDataTask dataTask, NSData data)
{
Console.WriteLine(data);
}
public override void DidCompleteWithError(NSUrlSession session, NSUrlSessionTask task, NSError error)
{
var uploadTask = task as NSUrlSessionUploadTask;
Console.WriteLine(error?.Description);
}
public override void DidReceiveResponse(NSUrlSession session, NSUrlSessionDataTask dataTask, NSUrlResponse response, Action<NSUrlSessionResponseDisposition> completionHandler)
{
Console.WriteLine(response);
}
public override void DidFinishEventsForBackgroundSession(NSUrlSession session)
{
using (AppDelegate appDelegate = UIApplication.SharedApplication.Delegate as AppDelegate)
{
var handler = appDelegate.BackgroundSessionCompletionHandler;
if (handler != null)
{
appDelegate.BackgroundSessionCompletionHandler = null;
handler();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有用的文档:
希望有人觉得这很有用,并且花在这上面的时间比我少。感谢@SushiHangover 为我指明了正确的方向。
| 归档时间: |
|
| 查看次数: |
150 次 |
| 最近记录: |