【发布时间】:2014-04-06 15:31:40
【问题描述】:
我使用 Azure 移动服务 API 作为后端。我需要将文件从 URL 存储到 Azure 移动服务 API 脚本中的 Azure 存储。
我真的不知道该怎么做:(
存储主题中有一些读数,但不是来自 Azure 移动服务 API。
请帮忙。
-------在cmets之后-----
嗯,我很满意。一切都在朝着正确的方向发展。 可以下载文件,我可以将它存储在 Storage 容器中,但在我的路上我做错了,因为当我从 Sotrage 查看文件时,它已损坏。
http://screencast.com/t/evBpmAVHQrbb http://screencast.com/t/H5Z0xzzsaH
var requestImageCallback = function (err, resp2, body) {
if (err || resp2.statusCode !== 200) {
console.error('Error sending data to the provider: ', err);
response.send(statusCodes.INTERNAL_SERVER_ERROR, body);
} else {
var type = resp2.headers["content-type"],
prefix = "data:" + type + ";base64,";
resp2.setEncoding('binary');
var base64 = new Buffer(body, 'binary').toString('base64'),
item = prefix + base64;
console.log(item);
var blobService = azure.createBlobService(accountName, accountKey, host);
blobService.createContainerIfNotExists("avatars", {
publicAccessLevel: 'blob'
}, function (error) {
if (!error) {
var sharedAccessPolicy = {
AccessPolicy: {
Permissions: azure.Constants.BlobConstants.SharedAccessPermissions.WRITE,
Expiry: new Date(new Date().getTime() + 5 * 60 * 1000)
}
};
var now = new Date();
var filename = now.getTime() + ".jpg";
var sasQueryUrl =
blobService.generateSharedAccessSignature("avatars",
filename, sharedAccessPolicy);
console.log(qs.stringify(sasQueryUrl.queryString));
console.log(sasQueryUrl.baseUrl + sasQueryUrl.path);
var options = {
contentType: type,
contentTypeHeader: type
};
blobService.createBlockBlobFromText('avatars', filename, item, options, function (error) {
if (!error) {
// Blob uploaded
}
});
} else {
console.error(error);
}
});
}
}
var http = require('request');
var reqOptions = {
uri: userData.picture.data.url,
headers: {
Accept: "image/jpeg, image/png"
}
};
http(reqOptions, requestImageCallback);
【问题讨论】:
-
您想获取一个公开可用的 URL,例如azure.microsoft.com/images/page/overview/preview-portal/…,从该 URL 读取内容(在本例中为图像)并将其放入 Azure Blob 存储。我的理解正确吗?
标签: azure azure-storage azure-blob-storage azure-mobile-services