Javascript 将文本文件上载到Azure blob存储返回内部服务器错误
我想创建一个应用程序,用户可以在其中将文本文件上载到Azure Blob存储。我用的是这个例子,但即使这样也不适用于我: 我对我的东西做了一些修改:Javascript 将文本文件上载到Azure blob存储返回内部服务器错误,javascript,c#,windows-phone-8,azure-storage,azure-mobile-services,Javascript,C#,Windows Phone 8,Azure Storage,Azure Mobile Services,我想创建一个应用程序,用户可以在其中将文本文件上载到Azure Blob存储。我用的是这个例子,但即使这样也不适用于我: 我对我的东西做了一些修改: [DataContract] public class WorkoutItem { [JsonProperty(PropertyName = "id")] public string Id { get; set; } [JsonProperty(PropertyName = "containerName")] p
[DataContract]
public class WorkoutItem
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "containerName")]
public string ContainerName { get; set; }
[JsonProperty(PropertyName = "resourceName")]
public string ResourceName { get; set; }
[JsonProperty(PropertyName = "sasQueryString")]
public string SasQueryString { get; set; }
[JsonProperty(PropertyName = "filePath")]
public string Filepath { get; set; }
}
我想上传一个txt文件,方法如下:
private async void InsertWorkoutItem(string localpath)
{
try
{
item = new WorkoutItem();
string errorString = string.Empty;
IsolatedStorageFileStream fileStream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(
localpath + ".txt", FileMode.Open, FileAccess.Read);
if (fileStream != null)
{
item.ContainerName = "workoutitemfiles";
item.ResourceName = localpath + ".txt";
}
await workouttable.InsertAsync(item); //exception thrown here
if (!string.IsNullOrEmpty(item.SasQueryString))
{
StorageCredentials cred = new StorageCredentials(item.SasQueryString);
var fileUri = new Uri(item.Filepath);
CloudBlobContainer container = new CloudBlobContainer(new Uri(
string.Format("https://{0}/{1}", fileUri.Host, item.ContainerName)), cred);
CloudBlockBlob blobFromSASCredential = container.GetBlockBlobReference(item.ResourceName);
await blobFromSASCredential.UploadFromStreamAsync(fileStream);
}
}
例外情况详情:
{Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: Error: Internal Server Error
at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.<ThrowInvalidResponse>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.<SendRequestAsync>d__1d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.<RequestAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.<>c__DisplayClass10.<<InsertAsync>b__f>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.<TransformHttpException>d__41.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.<InsertAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceTable`1.<TransformHttpException>d__38.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceTable`1.<InsertAsync>d__b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceTable`1.<InsertAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at GPS_BLRM67.ViewModel.MainViewModel.<InsertWorkoutItem>d__c.MoveNext()}
以下是服务器端插入脚本:
var azure = require('azure');
var qs = require('querystring');
var appSettings = require('mobileservice-config').appSettings;
function insert(item, user, request) {
// Get storage account settings from app settings.
var accountName = appSettings.STORAGE_ACCOUNT_NAME;
var accountKey = appSettings.STORAGE_ACCOUNT_ACCESS_KEY;
var host = accountName + '.blob.core.windows.net';
if ((typeof item.containerName !== "undefined") && (
item.containerName !== null)) {
// Set the BLOB store container name on the item, which must be lowercase.
item.containerName = item.containerName.toLowerCase();
// If it does not already exist, create the container
// with public read access for blobs.
var blobService = azure.createBlobService(accountName, accountKey, host);
blobService.createContainerIfNotExists(item.containerName, {
publicAccessLevel: 'blob'
}, function(error) {
if (!error) {
// Provide write access to the container for the next 5 mins.
var sharedAccessPolicy = {
AccessPolicy: {
Permissions: azure.Constants.BlobConstants.SharedAccessPermissions.WRITE,
Expiry: new Date(new Date().getTime() + 5 * 60 * 1000)
}
};
// Generate the upload URL with SAS for the new image.
var sasQueryUrl =
blobService.generateSharedAccessSignature(item.containerName,
item.resourceName, sharedAccessPolicy);
// Set the query string.
item.sasQueryString = qs.stringify(sasQueryUrl.queryString);
// Set the full path on the new new item,
// which is used for data binding on the client.
item.filePath = sasQueryUrl.baseUrl + sasQueryUrl.path;
} else {
console.error(error);
}
request.execute();
item.userId=user.userId;
});
} else {
request.execute();
}
}
我错过了什么?我在另一个线程中读到,可能没有将Windows Azure存储引用添加到项目中,但这不是我的问题。查看错误消息,我看到“”。看起来您的accountName变量可能未定义。你能试着打印并检查输出吗?是的,就是这样。不过谢谢你的帮助,现在它已经正常工作了。@fzl你是如何解决这个问题的,我想我也遇到了同样的问题,这两条线对我不起作用。var appSettings=require('mobileservice-config')。appSettings;var accountName=appSettings.STORAGE\u ACCOUNT\u NAME@对我来说,问题在于配置不当的移动服务。我转到我的Azure管理门户,选择您为应用程序创建的适当移动服务,转到“配置”选项卡,并将您的凭据写入应用程序设置。一个项目包含一个名称及其值的名称。在左侧,您需要给出可以在脚本中引用的名称,在本例中是其存储\帐户\名称。在它旁边,您应该写下您希望此项获得的值。希望你能用这个。
Error in script '/table/WorkoutItem.insert.js'. Error: {"message":"The provided settings {\"blobendpoint\":\"http://undefined.blob.core.windows.net/\",\"tableendpoint\":\"http://undefined.blob.core.windows.net/\",\"queueendpoint\":\"http://undefined.blob.core.windows.net/\"} are not complete."}
var azure = require('azure');
var qs = require('querystring');
var appSettings = require('mobileservice-config').appSettings;
function insert(item, user, request) {
// Get storage account settings from app settings.
var accountName = appSettings.STORAGE_ACCOUNT_NAME;
var accountKey = appSettings.STORAGE_ACCOUNT_ACCESS_KEY;
var host = accountName + '.blob.core.windows.net';
if ((typeof item.containerName !== "undefined") && (
item.containerName !== null)) {
// Set the BLOB store container name on the item, which must be lowercase.
item.containerName = item.containerName.toLowerCase();
// If it does not already exist, create the container
// with public read access for blobs.
var blobService = azure.createBlobService(accountName, accountKey, host);
blobService.createContainerIfNotExists(item.containerName, {
publicAccessLevel: 'blob'
}, function(error) {
if (!error) {
// Provide write access to the container for the next 5 mins.
var sharedAccessPolicy = {
AccessPolicy: {
Permissions: azure.Constants.BlobConstants.SharedAccessPermissions.WRITE,
Expiry: new Date(new Date().getTime() + 5 * 60 * 1000)
}
};
// Generate the upload URL with SAS for the new image.
var sasQueryUrl =
blobService.generateSharedAccessSignature(item.containerName,
item.resourceName, sharedAccessPolicy);
// Set the query string.
item.sasQueryString = qs.stringify(sasQueryUrl.queryString);
// Set the full path on the new new item,
// which is used for data binding on the client.
item.filePath = sasQueryUrl.baseUrl + sasQueryUrl.path;
} else {
console.error(error);
}
request.execute();
item.userId=user.userId;
});
} else {
request.execute();
}
}