Zephyrrr
Zephyrrr
CCConvex Community
Created by Zephyrrr on 5/28/2025 in #support-community
How to Get File Size in onUpload with Convex R2 When getMetadata Returns Null?
Thank you so much
27 replies
CCConvex Community
Created by Zephyrrr on 5/28/2025 in #support-community
How to Get File Size in onUpload with Convex R2 When getMetadata Returns Null?
It works like a charm!
27 replies
CCConvex Community
Created by Zephyrrr on 5/28/2025 in #support-community
How to Get File Size in onUpload with Convex R2 When getMetadata Returns Null?
good point, I'll try it
27 replies
CCConvex Community
Created by Zephyrrr on 5/28/2025 in #support-community
How to Get File Size in onUpload with Convex R2 When getMetadata Returns Null?
I see, so it is expected behavior.
27 replies
CCConvex Community
Created by Zephyrrr on 5/28/2025 in #support-community
How to Get File Size in onUpload with Convex R2 When getMetadata Returns Null?
I am using clerk btw, and I can get identify from ctx in other functions
27 replies
CCConvex Community
Created by Zephyrrr on 5/28/2025 in #support-community
How to Get File Size in onUpload with Convex R2 When getMetadata Returns Null?
@erquhart am I missing something?
27 replies
CCConvex Community
Created by Zephyrrr on 5/28/2025 in #support-community
How to Get File Size in onUpload with Convex R2 When getMetadata Returns Null?
Logs:
5/27/2025, 10:10:45 PM [CONVEX M(r2:onSyncMetadata)] [LOG] {
metadata: {
bucket: 'yxxxxx',
bucketLink: 'https://dash.cloudflare.com/fe8f2b07ab2xxxxxcef93995b405e00/r2/default/buckets/xxxxx',
contentType: 'image/webp',
key: '7bdc4f1c-cxxxx6fd-2957e8c07642',
lastModified: '2025-05-28T05:10:45.000Z',
link: 'https://dash.cloudflare.com/fe8f2b07ab2xxxxef93995b405e00/r2/default/buckets/yixxxst/objects/7bdc4f1c-xxxxx-86fd-2957e8c07642/details',
size: 11522,
url: 'https://xyxx-xxx.fe8f2b07ab27d9xx3995b405e00.r2.cloudflarestorage.com/7bdc4f1c-c6ac-4385-8xx8c07642?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=d60ccad9b8695062d7d010ed21cbcc3a%2F20250528%2Fauto%2Fs3%2Faws4_request&X-Amz-Date=20250528T051045Z&X-Amz-Expires=900&X-Amz-Signature=6b860bdb18ef3c583ad9a3c8a1add793b490bb963be7e3a18275117d91f59fb5&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject'
},
user: null,
identity: null
}
5/27/2025, 10:10:45 PM [CONVEX M(r2:onSyncMetadata)] [LOG] {
metadata: {
bucket: 'yxxxxx',
bucketLink: 'https://dash.cloudflare.com/fe8f2b07ab2xxxxxcef93995b405e00/r2/default/buckets/xxxxx',
contentType: 'image/webp',
key: '7bdc4f1c-cxxxx6fd-2957e8c07642',
lastModified: '2025-05-28T05:10:45.000Z',
link: 'https://dash.cloudflare.com/fe8f2b07ab2xxxxef93995b405e00/r2/default/buckets/yixxxst/objects/7bdc4f1c-xxxxx-86fd-2957e8c07642/details',
size: 11522,
url: 'https://xyxx-xxx.fe8f2b07ab27d9xx3995b405e00.r2.cloudflarestorage.com/7bdc4f1c-c6ac-4385-8xx8c07642?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=d60ccad9b8695062d7d010ed21cbcc3a%2F20250528%2Fauto%2Fs3%2Faws4_request&X-Amz-Date=20250528T051045Z&X-Amz-Expires=900&X-Amz-Signature=6b860bdb18ef3c583ad9a3c8a1add793b490bb963be7e3a18275117d91f59fb5&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject'
},
user: null,
identity: null
}
27 replies
CCConvex Community
Created by Zephyrrr on 5/28/2025 in #support-community
How to Get File Size in onUpload with Convex R2 When getMetadata Returns Null?
It seems that I can't get auth info from the context
export const r2 = new R2(components.r2);
const callbacks: R2Callbacks = internal.r2;

export const { generateUploadUrl, syncMetadata, onSyncMetadata } = r2.clientApi(
{
callbacks,
checkUpload: async (ctx, bucket) => {
const user = await ctx.runQuery(api.users.current);
if (!user) {
throw new ConvexError("unauthorized");
}
const totalStorageStats = await ctx.runQuery(api.r2.getTotalStorageStats);

if (
totalStorageStats.totalSizeMB >
(totalStorageStats.storageLimit ?? 0) * 1024
) {
throw new ConvexError("storage full");
}
},
onSyncMetadata: async (ctx, args) => {
const user = await ctx.runQuery(api.users.current);
const identity = await ctx.auth.getUserIdentity()

const metadata = await r2.getMetadata(ctx, args.key);

console.log({ metadata, user, identity });
if (!user) {
throw new ConvexError("unauthorized");
}

if (!metadata) {
throw new ConvexError("file not found");
}

await totalStorageAggregate.insert(ctx, {
key: user._id,
id: args.key,
sumValue: metadata.size,
});
},
onDelete: async (ctx, key) => {
const user = await ctx.runQuery(api.users.current);
if (!user) {
throw new ConvexError("unauthorized");
}

await totalStorageAggregate.delete(ctx, {
key: user._id,
id: key,
});
},
}
);
export const r2 = new R2(components.r2);
const callbacks: R2Callbacks = internal.r2;

export const { generateUploadUrl, syncMetadata, onSyncMetadata } = r2.clientApi(
{
callbacks,
checkUpload: async (ctx, bucket) => {
const user = await ctx.runQuery(api.users.current);
if (!user) {
throw new ConvexError("unauthorized");
}
const totalStorageStats = await ctx.runQuery(api.r2.getTotalStorageStats);

if (
totalStorageStats.totalSizeMB >
(totalStorageStats.storageLimit ?? 0) * 1024
) {
throw new ConvexError("storage full");
}
},
onSyncMetadata: async (ctx, args) => {
const user = await ctx.runQuery(api.users.current);
const identity = await ctx.auth.getUserIdentity()

const metadata = await r2.getMetadata(ctx, args.key);

console.log({ metadata, user, identity });
if (!user) {
throw new ConvexError("unauthorized");
}

if (!metadata) {
throw new ConvexError("file not found");
}

await totalStorageAggregate.insert(ctx, {
key: user._id,
id: args.key,
sumValue: metadata.size,
});
},
onDelete: async (ctx, key) => {
const user = await ctx.runQuery(api.users.current);
if (!user) {
throw new ConvexError("unauthorized");
}

await totalStorageAggregate.delete(ctx, {
key: user._id,
id: key,
});
},
}
);
27 replies
CCConvex Community
Created by Zephyrrr on 5/28/2025 in #support-community
How to Get File Size in onUpload with Convex R2 When getMetadata Returns Null?
cool, will check it out once have time!
27 replies
CCConvex Community
Created by Zephyrrr on 5/28/2025 in #support-community
How to Get File Size in onUpload with Convex R2 When getMetadata Returns Null?
makes sense, never trust client. Look forward to seeing it🫡
27 replies
CCConvex Community
Created by Zephyrrr on 5/28/2025 in #support-community
How to Get File Size in onUpload with Convex R2 When getMetadata Returns Null?
yes
27 replies
CCConvex Community
Created by Zephyrrr on 5/28/2025 in #support-community
How to Get File Size in onUpload with Convex R2 When getMetadata Returns Null?
that'll be great👍 I think size is all I need
27 replies
CCConvex Community
Created by Zephyrrr on 5/28/2025 in #support-community
How to Get File Size in onUpload with Convex R2 When getMetadata Returns Null?
yes, but there is no afterUpload() hook available is there any workaround?🥲
27 replies