Skip to content

Commit

Permalink
add get s3 data
Browse files Browse the repository at this point in the history
  • Loading branch information
panyongxu1002 committed Apr 28, 2024
1 parent cd8b33a commit 7e69b32
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/api/src/routers/blob/common/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const serializedBaseBlobSchema = z.object({
dataStorageReferences: z.array(serializedBlobDataStorageReferenceSchema),
});


export type SerializedBaseBlob = z.infer<typeof serializedBaseBlobSchema>;

export const serializedBlobOnTransactionSchema = serializedBaseBlobSchema.merge(
Expand Down Expand Up @@ -165,3 +166,36 @@ export function serializeBlob(blob: Blob): SerializedBlob {
}),
};
}

export type BlobSchema = {
data: string;
};

export function serializeBlobSchema(blob: Blob): any {
console.log("🚀 ~ serializeBlobSchema ~ blob:", blob)
const { transactions, ...baseBlob } = blob;
return {
data: blob.data
}

// return {
// ...serializeBaseBlob(baseBlob),
// data: blob.data,
// transactions: transactions
// .sort((a, b) => a.transaction.hash.localeCompare(b.transaction.hash))
// .map(({ index, transaction }) => {
// const { block, hash } = transaction;
// const { number } = block;

// return {
// index,
// hash,
// ...serializeExpandedTransaction(transaction),
// block: {
// number,
// ...serializeExpandedBlock(block),
// },
// };
// }),
// };
}
46 changes: 46 additions & 0 deletions packages/api/src/routers/blob/getByVersionedHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { TRPCError } from "@trpc/server";

import { z } from "@blobscan/zod";

import {
createExpandsSchema,
withExpands,
} from "../../middlewares/withExpands";
import { publicProcedure } from "../../procedures";
import { retrieveBlobData } from "../../utils";
import { get_AWS_S3 } from "../../utils/aws_s3_blob";
import { createBlobSelect } from "./common/selects";
import { serializeBlob, serializeBlobSchema } from "./common/serializers";

const inputSchema = z
.object({
versionedHash: z.string(),
})
.merge(createExpandsSchema(["transaction", "block"]));

// 定义Zod验证器
const outputSchema = z.object({
data: z.any(),
});

export const getByVersionedHash = publicProcedure
.meta({
openapi: {
method: "GET",
path: "/blobs/versionedHash/{versionedHash}",
tags: ["blobs"],
summary:
"retrieves blob details for given versioned hash or kzg commitment.",
},
})
.input(inputSchema)
.use(withExpands)
.output(outputSchema)
.query(async ({ input }: any) => {
const { versionedHash } = input;
const blobData = await get_AWS_S3({ versionedHash });
// console.log("🚀 ~ .query ~ blobData:", blobData);
return {
data: blobData,
};
});
2 changes: 2 additions & 0 deletions packages/api/src/routers/blob/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { t } from "../../trpc-client";
import { getAll } from "./getAll";
import { getByBlobId } from "./getByBlobId";
import { getByVersionedHash } from "./getByVersionedHash";

export const blobRouter = t.router({
getAll,
getByBlobId,
getByVersionedHash,
});
20 changes: 20 additions & 0 deletions packages/api/src/utils/aws_s3_blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ListBucketsCommand,
S3Client,
PutObjectCommand,
GetObjectCommand,
} from "@aws-sdk/client-s3";
import fs from "fs";
import { fileURLToPath } from "url";
Expand Down Expand Up @@ -50,3 +51,22 @@ export const upload_AWS_S3 = async ({ versionedHash, data }: any) => {
fs.unlinkSync(`${versionedHash}.txt`);
}
};

// 执行上传操作
export const get_AWS_S3 = async ({ versionedHash }: any) => {
// console.log("🚀 ~ constupload_AWS_S3= ~ fileStream:", fileStream);
// 设置 S3 中的文件名称和路径
const getParams = {
Bucket: process.env.AWS_S3_STORAGE_BUCKET_NAME, // 替换为你的S3存储桶名称
Key: `${versionedHash}.txt`, // 文件名及路径
};
try {
const response: any = await s3Client.send(new GetObjectCommand(getParams));
const str = await response.Body.transformToString();
// console.log("🚀 ~ constupload_AWS_S3= ~ getParams: ", str);
return str;
} catch (err) {
console.error("Error uploading file:", err);
} finally {
}
};

0 comments on commit 7e69b32

Please sign in to comment.