@@ -47,7 +47,8 @@ export default defineConfig({
47
47
## Task code
48
48
49
49
``` ts trigger/sharp-image-processing.ts
50
- import { PutObjectCommand , S3Client } from " @aws-sdk/client-s3" ;
50
+ import { S3Client } from " @aws-sdk/client-s3" ;
51
+ import { Upload } from " @aws-sdk/lib-storage" ;
51
52
import { logger , task } from " @trigger.dev/sdk/v3" ;
52
53
import fs from " fs/promises" ;
53
54
import os from " os" ;
@@ -66,64 +67,51 @@ const r2Client = new S3Client({
66
67
67
68
export const sharpProcessImage = task ({
68
69
id: " sharp-process-image" ,
69
- // Only retry this task once if it fails
70
- retry: {
71
- maxAttempts: 1 ,
72
- },
70
+ retry: { maxAttempts: 1 },
73
71
run : async (payload : { imageUrl: string ; watermarkUrl: string }) => {
74
72
const { imageUrl, watermarkUrl } = payload ;
73
+ const outputPath = path .join (os .tmpdir (), ` output_${Date .now ()}.jpg ` );
75
74
76
- // Generate temporary and output file names
77
- const tempDirectory = os .tmpdir ();
78
- const outputPath = path .join (tempDirectory , ` output_${Date .now ()}.jpg ` );
79
-
80
- // Fetch the image and watermark
81
75
const [imageResponse, watermarkResponse] = await Promise .all ([
82
76
fetch (imageUrl ),
83
77
fetch (watermarkUrl ),
84
78
]);
85
79
const imageBuffer = await imageResponse .arrayBuffer ();
86
80
const watermarkBuffer = await watermarkResponse .arrayBuffer ();
87
81
88
- // Process the image using Sharp
89
82
await sharp (Buffer .from (imageBuffer ))
90
- .resize (800 , 800 ) // Resize the image to 800x800
83
+ .resize (800 , 800 ) // Resize the image to 800x800px
91
84
.composite ([
92
85
{
93
86
input: Buffer .from (watermarkBuffer ),
94
87
gravity: " southeast" , // Position the watermark in the bottom-right corner
95
88
},
96
89
])
97
- .toFormat (" jpeg" )
98
- .toFile (outputPath );
99
-
100
- logger .log (` Image processed. ` );
101
-
102
- // Read the processed image file
103
- const processedImageBuffer = await fs .readFile (outputPath );
104
-
105
- // Upload the image to R2, replacing slashes with underscores
106
- const r2Key = ` processed-images/${path .basename (outputPath )} ` ;
107
-
108
- const uploadParams = {
109
- Bucket: process .env .R2_BUCKET ,
110
- Key: r2Key ,
111
- Body: processedImageBuffer ,
112
- };
113
-
114
- // Upload the image to R2 and get the URL
115
- await r2Client .send (new PutObjectCommand (uploadParams ));
116
- logger .log (" Processed image uploaded to R2" , {
117
- path: ` /${process .env .R2_BUCKET }/${r2Key } ` ,
118
- });
119
-
120
- // Delete the temporary file
121
- await fs .unlink (outputPath );
122
- logger .log (" Temporary file deleted" , { outputPath: outputPath });
123
-
124
- return {
125
- message: ` New image uploaded to /${process .env .R2_BUCKET }/${r2Key } ` ,
126
- };
90
+ .jpeg () // Convert to jpeg
91
+ .toBuffer () // Convert to buffer
92
+ .then (async (outputBuffer ) => {
93
+ await fs .writeFile (outputPath , outputBuffer ); // Write the buffer to file
94
+
95
+ const r2Key = ` processed-images/${path .basename (outputPath )} ` ;
96
+ const uploadParams = {
97
+ Bucket: process .env .R2_BUCKET ,
98
+ Key: r2Key ,
99
+ Body: await fs .readFile (outputPath ),
100
+ };
101
+
102
+ const upload = new Upload ({
103
+ client: r2Client ,
104
+ params: uploadParams ,
105
+ });
106
+
107
+ await upload .done ();
108
+ logger .log (" Image uploaded to R2 storage." , {
109
+ path: ` /${process .env .R2_BUCKET }/${r2Key } ` ,
110
+ });
111
+
112
+ await fs .unlink (outputPath ); // Clean up the temporary file
113
+ return { r2Key };
114
+ });
127
115
},
128
116
});
129
117
```
0 commit comments