我正在嘗試在 Amazon MTurk 上創建一個任務,作業人員將在其中收集一些資料并在準備好并提交任務時上傳單個檔案。提交任務后,我想將檔案上傳到我鏈接的 S3 存盤桶 - 這主要基于
如何在任務完成前確保檔案上傳完成?我看到我可以覆寫 MTurk 添加的默認提交按鈕,但如果可能的話,我寧愿不這樣做。
uj5u.com熱心網友回復:
我發現了問題:S3#upload回傳一個ManagedUpload物件,但并不意味著檔案上傳完成。我現在正在使用承諾,并在回呼中手動提交表單。請注意,該表單默認由 MTurk 提??供。我只是通過它的 ID 找到它并submit手動呼叫該函式。
作為參考,這里是作業代碼:
<script>
let config = {
region: 'xxx',
pool: 'xxx',
bucket: 'xxx'
}
AWS.config.region = config.region;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: config.pool,
});
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
params: {Bucket: config.bucket},
});
start_upload = function (event) {
$("#status").text("Uploading, please wait...");
let file = $("#file").prop('files')[0];
if (file === null || file === undefined) {
alert("You must choose a file before submitting.");
$("#status").text("");
return false;
}
let workerId = turkGetParam('workerId');
let fileKey = '${food_name}' '/' workerId '-' file.name;
upload_to_s3(file, fileKey);
return false;
};
upload_to_s3 = (file, fileKey) => {
const params = {
Key: fileKey,
Body: file,
ContentType: file.type,
ACL: 'bucket-owner-full-control'
};
let promise = s3.upload(params).promise();
promise.then( (data) => {
console.log("Upload completed");
$("#status").text("Success.");
const form = document.getElementById('mturk_form');
form.submit();
}, (err) => {
console.log("Upload failed!!!", err);
alert("Failed to upload, please try again. If the problem persists, contact the Requester.");
$("#status").text("");
} );
}
// Validate and upload file on submit
window.onload = function() {document.getElementById('submitButton').setAttribute('onclick', 'return start_upload()'); }
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/351603.html
標籤:javascript 亚马逊-s3 机械土耳其人 米图尔克
下一篇:PHP物件中的if陳述句
