使用 AWS lambda 将多表“.xls”文件上传到 AWS S3

发布于 2025-01-17 05:50:08 字数 661 浏览 3 评论 0原文

我正在 AWS lambda 中创建一个多表 .xls 文件并将其推送到 S3 存储桶。在本地测试文件创建时,文件符合预期并且可以打开。

当我拉取加载到 S3 的文件时,尝试在 MS Excel 中打开该文件时出现以下错误。

The file format and extension of don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway? s3 xlsx

我仍然可以打开该文件,但无法编辑该文件。

以下是我用来将文件推送到 S3 的代码。该错误似乎与内容类型有关。我想知道要使用的正确内容类型是什么。

const file = await this.loadFile('test.xls', '/tmp');
const input = {
    Bucket: BUCKET_NAME,
    Key: 'output/test.xls',
    Body: file,
    ContentType: 'application/vnd.ms-excel'
};
await client.putObject(input);

I am creating a multi sheet .xls file in AWS lambda and pushing it to S3 Bucket. On testing the file creation in local, the file is as expected and can be opened.

When I pull the file that is loaded to S3 I am getting the following error, trying to open the file in MS excel.

The file format and extension of don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway? s3 xlsx

I can still open the file but can't edit that file.

Following is the code I used to push the file to S3. The error seems to be with the content type. I am wondering what is the correct content type to be used.

const file = await this.loadFile('test.xls', '/tmp');
const input = {
    Bucket: BUCKET_NAME,
    Key: 'output/test.xls',
    Body: file,
    ContentType: 'application/vnd.ms-excel'
};
await client.putObject(input);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独享拥抱 2025-01-24 05:50:08

这可能不是您想要的,但在将任何类型的文件上传到 S3 存储桶时,使用 Buffer.from(file, "binary") 都可以。

这是我的片段:

var AWS = require("aws-sdk");

// Set region
AWS.config.update({ region: "your-region" });

var s3 = new AWS.S3({ apiVersion: "2006-03-01" });

var fs = require("fs");

// Set bucket name
const bucket = "my-bucket";
const file = "test.xlsx";

fs.readFile(file, function (err, data) {
    if (err) console.log(err);
    const fileContent = Buffer.from(file, "binary");
    s3.putObject(
        {
            Bucket: bucket,
            Key: file,
            Body: fileContent,
        },
        function (resp) {
            console.log(arguments);
            console.log("Successfully uploaded, ", file);
        }
    );
});

This might not be what you want, but using Buffer.from(file, "binary") will work when uploading files of any types to S3 buckets.

Here is my snippet:

var AWS = require("aws-sdk");

// Set region
AWS.config.update({ region: "your-region" });

var s3 = new AWS.S3({ apiVersion: "2006-03-01" });

var fs = require("fs");

// Set bucket name
const bucket = "my-bucket";
const file = "test.xlsx";

fs.readFile(file, function (err, data) {
    if (err) console.log(err);
    const fileContent = Buffer.from(file, "binary");
    s3.putObject(
        {
            Bucket: bucket,
            Key: file,
            Body: fileContent,
        },
        function (resp) {
            console.log(arguments);
            console.log("Successfully uploaded, ", file);
        }
    );
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文