'未捕获类型错误:t 不是构造函数'

问题描述 投票:0回答:1

我在Vite React js项目中使用aws-sdk。

构建工作正常。

在生产中返回“未捕获类型错误:t不是构造函数” 如何解决这个问题?我对此一无所知。

我已经尝试过了。

这是我的代码:

import { S3, Endpoint } from 'aws-sdk';
// out 
const s3 = new S3({
  accessKeyId: import.meta.env.VITE_WASABI_ACCESS_KEY,
  secretAccessKey: import.meta.env.VITE_WASABI_SECRET_KEY,
  s3ForcePathStyle: true,
  endpoint: new Endpoint('https://s3.ap-northeast-2.wasabisys.com'),
});

// in component
  const handleUploadFile = async (
    file: File | string | undefined | null,
    type: 'identified' | 'deidentified',
  ): Promise<string> => {
    if (!file || typeof file === 'string') return file || '';
    if (!file?.type) return file.name;
    const [compFile, compExt] = await compress(file);
    const filename = fileName(file.name, compExt);
    const filePath = `subjects/${id}/${type}/${filename}`;
    const uploadParams = {
      Bucket: import.meta.env.VITE_WASABI_BUCKET_NAME,
      Key: filePath,
      Body: compFile,
      ACL: 'public-read',
    };
    const uploadRequest = s3.upload(uploadParams);
    return new Promise((res, rej) => {
      uploadRequest.on('httpUploadProgress', function (evt) {
        const progress = evt.loaded / (evt.total ?? Number.MAX_SAFE_INTEGER);
        if (progress < 1) {
          return toast.loading(
            <div className='flex flex-col gap-2'>
              <p className='text-xs'>Uploading {file.name}</p>
              <Progress progress={Math.ceil(progress * 100)} />
            </div>,
            { id: file.name },
          );
        }
        toast.success(<p className='text-xs'>Uploaded {file.name}</p>, { id: file.name });
      });

      uploadRequest.send(function (err, data) {
        if (err) {
          console.error('Error', err);
          rej(err.message || 'failed');
        }
        if (data) {
          res(filePath);
        }
      });
    });
  };

导入S3传播没有帮助

Vite配置

import path from 'path';
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
import react from '@vitejs/plugin-react';
import nodePolyfills from 'rollup-plugin-polyfill-node';
import { defineConfig } from 'vite';
import codegen from 'vite-plugin-graphql-codegen';
import pluginRewriteAll from 'vite-plugin-rewrite-all';
import svgr from 'vite-plugin-svgr';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), svgr(), codegen(), pluginRewriteAll()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
  optimizeDeps: {
    exclude: ['@jsquash/jpeg', '@jsquash/png', '@jsquash/webp', '@jsquash/resize'],
    esbuildOptions: {
      define: {
        global: 'globalThis'
      },
      plugins: [
        NodeGlobalsPolyfillPlugin({
          buffer: true,
          process: true,
        }),
      ],
    }
  },
  build: {
    rollupOptions: {
      plugins: [nodePolyfills()],
    },
  },
});

SOURCEMAP:插件中默认 true。

sourcemap:true 和扩展导入都不起作用

reactjs amazon-s3 aws-sdk vite vite-reactjs
1个回答
0
投票

使用 s3 客户端

这是代码:

import { S3Client } from '@aws-sdk/client-s3';

const s3Client = new S3Client({
  requestHandler: new XhrHttpHandler({}),
  region: 'ap-northeast-2', // Replace with your Wasabi region
  credentials: {
    accessKeyId: import.meta.env.VITE_WASABI_ACCESS_KEY, // Replace with your Wasabi access key
    secretAccessKey: import.meta.env.VITE_WASABI_SECRET_KEY, // Replace with your Wasabi secret key
  },
  endpoint: 'https://s3.ap-northeast-2.wasabisys.com', // Wasabi endpoint
});
© www.soinside.com 2019 - 2024. All rights reserved.