// @ts-ignore
import { S3Client, GetObjectCommand, PutObjectCommand, CreateBucketCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import {utils} from "@fast-crud/fast-crud";
// TODO  模拟server， 你应该将此代码搬到你的server端
let bucketCreated = false;
export async function generateSignedUrl(bucket: string, key: string, type: "put" | "get" = "get") {
  const client = new S3Client({
    // s3ForcePathStyle: true,
    // signatureVersion: "v4",
    region: "us-east-1",
    forcePathStyle: true,
    endpoint: "https://play.min.io",
    credentials: {
      accessKeyId: "Q3AM3UQ867SPQQA43P2F", //访问登录名
      secretAccessKey: "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG" //访问密码
    }
  });

  try {
    if (!bucketCreated) {
      bucketCreated = true;
      const createCmd = new CreateBucketCommand({ Bucket: bucket });
      await client.send(createCmd);
    }
  } catch (e) {}

  const params = {
    Bucket: bucket,
    Key: key
  };
  let url;
  let cmd;
  if (type === "get") {
    cmd = new GetObjectCommand(params);
  } else {
    cmd = new PutObjectCommand(params);
  }

  try {
    url = await getSignedUrl(client, cmd);
  } catch (err) {
    utils.logger.info("Error getting signed URL ", err);
  }

  return url;
}
