-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.ts
158 lines (139 loc) · 5.14 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import express from 'express';
import * as minio from 'minio';
import * as dotenv from 'dotenv';
import crypto from 'crypto';
dotenv.config()
const app = express();
const bucketName = 'example-bucket'
let minioClient: minio.Client;
const init = async () => {
// STORAGE_ENDPOINT, STORAGE_PORT, STORAGE_USER, STORAGE_PASSWORD, STORAGE_USE_SSL are set by Zeabur
// Once you deploy a MinIO service in the same project with this app,
// Zeabur will automatically set these environment variables for you.
let endPoint = process.env.STORAGE_ENDPOINT
if (!endPoint) {
console.info('STORAGE_ENDPOINT is not set. Did you deploy a MinIO service?')
console.info('If you are running this app locally, you can get the endpoint from the "domain" tab of MinIO service in the Zeabur dashboard.')
process.exit(1)
}
let portStr = process.env.STORAGE_PORT
if (!portStr) {
console.info('STORAGE_PORT is not set. Did you deploy a MinIO service?')
console.info('If you are running this app locally, you can get the port from the "domain" tab of MinIO service in the Zeabur dashboard.')
process.exit(1)
}
const port = parseInt(portStr)
const accessKey = process.env.STORAGE_USER
if (!accessKey) {
console.info('STORAGE_USER is not set. Did you deploy a MinIO service?')
console.info('If you are running this app locally, you can get the access key from the "connect" tab of MinIO service in the Zeabur dashboard.')
process.exit(1)
}
const secretKey = process.env.STORAGE_PASSWORD
if (!secretKey) {
console.info('STORAGE_PASSWORD is not set. Did you deploy a MinIO service?')
console.info('If you are running this app locally, you can get the secret key from the "connect" tab of MinIO service in the Zeabur dashboard.')
process.exit(1)
}
const useSSLStr = process.env.STORAGE_USE_SSL
if(useSSLStr === undefined) {
console.info('STORAGE_USE_SSL is not set. Did you deploy a MinIO service?')
console.info('If you are running this app locally, you can get the useSSL value from the "connect" tab of MinIO service in the Zeabur dashboard.')
process.exit(1)
}
const useSSL = useSSLStr === 'true'
// create a MinIO client with credentials from Zeabur
console.info('Connecting to MinIO storage...')
minioClient = new minio.Client({endPoint, accessKey, secretKey, port, useSSL})
console.info('Connected!')
// check if the bucket exists, if not, create it
console.info('Checking if bucket exists...')
const bucketExists = await minioClient.bucketExists(bucketName)
if (!bucketExists) {
console.info('Bucket does not exist, creating...')
await minioClient.makeBucket(bucketName)
console.info('Bucket created!')
console.info('Setting bucket policy to allow all read...')
const policyAllowAllRead = {
Version: '2012-10-17',
Id: 'allow-all-read',
Statement: [
{
Action: ['s3:GetObject'],
Effect: 'Allow',
Principal: {
AWS: ['*'],
},
Resource: ['arn:aws:s3:::' + bucketName +'/*'],
},
],
};
await minioClient.setBucketPolicy(bucketName, JSON.stringify(policyAllowAllRead))
console.info('Policy set!')
} else {
console.info('Bucket exists!')
}
}
app.get('/', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' })
res.end(`
<html lang="en-US">
<head>
<title>Express MinIO example</title>
</head>
<body>
<h1>Express MinIO example</h1>
<p>Run following bash command to create a text file:</p>
<pre>echo "Hello World" > hello.txt</pre>
<p>Then, run following curl command to upload the file to MinIO storage.</p>
<pre>curl -X POST -T hello.txt https://minio-express-example.zeabur.app/upload</pre>
</body>
</html>
`)
})
app.post('/upload', async (req, res) => {
const randomFileName = crypto.randomUUID()
await minioClient.putObject(bucketName, randomFileName, req)
res.end('Your file is now available at https://minio-example.zeabur.app/objects/' + randomFileName + ' !')
})
app.get('/objects/:objectName', async (req, res) => {
const objectName = req.params.objectName
const stream = await minioClient.getObject(bucketName, objectName)
stream.pipe(res)
})
app.get('/objects', async (req, res) => {
const stream = await minioClient.listObjects(bucketName)
let objects: minio.BucketItem[] = []
stream.on('data', (obj) => {
objects.push(obj)
})
stream.on('end', () => {
res.writeHead(200, { 'Content-Type': 'text/html' })
res.write(`
<html lang="en-US">
<head>
<title>Express MinIO example</title>
</head>
<body>
<h1>Express MinIO example</h1>
<p>Here are the files you uploaded:</p>
<ul>
${objects.map(object => `<li><a href="/objects/${object.name}">${object.name}</a></li>`).join('')}
</ul>
</body>
</html>
`)
res.end()
})
stream.on('error', (err) => {
console.error(err)
res.status(500).end('Something went wrong!')
})
})
const main = async () => {
await init();
app.listen(process.env.PORT || 3000 , async () => {
console.log('Server started on port ' + (process.env.PORT || 3000))
});
}
main();