[英]How to upload a file to S3 bucket using "BatchPutDocument" service?
we are running a javascript application with a search bar.我们正在运行一个带有搜索栏的 javascript 应用程序。 The content is searchable from S3 bucket of AWS.
内容可从 AWS 的 S3 存储桶中搜索。 JavaScript application code:
JavaScript 应用程序代码:
<html>
<head>
<script src="aws-sdk-2.604.0.js"></script>
<script>
var kendra = new AWS.Kendra({
apiVersion: '2019-02-03',
// region: '*****',
accessKeyId: '*********',
secretAccessKey: '*******************'
});
function getResults() {
var searchText = document.getElementById("searchBox").value;
var params = {
IndexId: '**************************',
/* required */
QueryText: searchText,
PageNumber: '1',
PageSize: '20'
};
kendra.query(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
document.getElementById("results").innerText = data.ResultItems[0].DocumentTitle.Text;
console.log(data); // successful response
}
});
}
</script>
</head>
<body>
<h1>This is Kendra application</h1>
<input type="text" id="searchBox" />
<button onclick="getResults()">Search</button>
<div>
<b>search results</b>
<div id="results"></div>
</div>
</body>
</html>
The above javascript application includes a search bar which gives data from S3 bucket.上面的 javascript 应用程序包含一个搜索栏,它提供来自 S3 存储桶的数据。 Now we need to upload a new file using the "BatchPutDocument" Service code give below.
现在我们需要使用下面给出的“BatchPutDocument”服务代码上传一个新文件。 The uploaded document(html or pdf) should be searchable in the javascript application search box.
上传的文档(html 或 pdf)应该可以在 javascript 应用程序搜索框中搜索到。
Note: we should not directly upload file in s3 bucket by going to AWS s3 bucket.注意:我们不应该通过转到 AWS s3 存储桶直接上传 s3 存储桶中的文件。 Is the code give below helpful or any alternative code available.
下面给出的代码是否有帮助或是否有任何可用的替代代码。
var params = {
Documents: [ /* required */
{
Id: 'STRING_VALUE', /* required */
AccessControlList: [
{
Access: ALLOW | DENY, /* required */
Name: 'STRING_VALUE', /* required */
Type: USER | GROUP /* required */
},
/* more items */
],
Attributes: [
{
Key: 'STRING_VALUE', /* required */
Value: { /* required */
DateValue: new Date || 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)' || 123456789,
LongValue: 'NUMBER_VALUE',
StringListValue: [
'STRING_VALUE',
/* more items */
],
StringValue: 'STRING_VALUE'
}
},
/* more items */
],
Blob: Buffer.from('...') || 'STRING_VALUE' /* Strings will be Base-64 encoded on your behalf */,
ContentType: PDF | HTML | MS_WORD | PLAIN_TEXT | PPT,
S3Path: {
Bucket: 'STRING_VALUE', /* required */
Key: 'STRING_VALUE' /* required */
},
Title: 'STRING_VALUE'
},
/* more items */
],
IndexId: 'STRING_VALUE', /* required */
RoleArn: 'STRING_VALUE'
};
kendra.batchPutDocument(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
AWS Kendra allows you to index the documents in your Kendra index using available Connectors it offers or using BatchPutDocument API . AWS Kendra 允许您使用它提供的可用 连接器或使用BatchPutDocument API为 Kendra 索引中的文档编制索引。 When using BatchPutDocument API you can either pass the document inline to Kendra or pass an S3 URL of your document that you want to index.
使用BatchPutDocument API时,您可以将文档内联传递给 Kendra,也可以传递要索引的文档的 S3 URL 。 Once you have indexed your documents successfully, you will be able to perform a search using Kendra's Query API .
成功为文档编制索引后,您将能够使用 Kendra 的查询 API执行搜索。 I see that you already have some Javascript code to invoke Kendra's Query API, however, you could also refer to Kendra's provided sample React application if you are interested in building a React app.
我看到您已经有一些 Javascript 代码来调用 Kendra 的查询 API,但是,如果您有兴趣构建 React 应用程序,也可以参考Kendra 提供的示例 React 应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.