使用Docker容器(Node.js)尝试访问云存储
NodeJS 是一种流行的开发工具,用于构建可扩展的网络应用程序。
import { Storage } from '@google-cloud/storage';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const express = require('express');
const app = express();
//const storage = new Storage({projectId: 'soe-tkmtk-dev-is', keyFilename: './key.json'});
const storage = new Storage();
const bucket = storage.bucket('htkmtk-dev-is-bucket');
async function getMetadata(fileName) {
// Gets the metadata for the file
const contents = await bucket.file(fileName).download()
const [metadata] = await bucket.file(fileName).getMetadata();
console.log("contents:", contents[0])
console.log(`Bucket: ${metadata.bucket}`);
console.log(`CacheControl: ${metadata.cacheControl}`);
console.log(`ComponentCount: ${metadata.componentCount}`);
console.log(`ContentDisposition: ${metadata.contentDisposition}`);
console.log(`ContentEncoding: ${metadata.contentEncoding}`);
console.log(`ContentLanguage: ${metadata.contentLanguage}`);
console.log(`ContentType: ${metadata.contentType}`);
console.log(`CustomTime: ${metadata.customTime}`);
console.log(`Crc32c: ${metadata.crc32c}`);
console.log(`ETag: ${metadata.etag}`);
console.log(`Generation: ${metadata.generation}`);
console.log(`Id: ${metadata.id}`);
console.log(`KmsKeyName: ${metadata.kmsKeyName}`);
console.log(`Md5Hash: ${metadata.md5Hash}`);
console.log(`MediaLink: ${metadata.mediaLink}`);
console.log(`Metageneration: ${metadata.metageneration}`);
console.log(`Name: ${metadata.name}`);
console.log(`Size: ${metadata.size}`);
console.log(`StorageClass: ${metadata.storageClass}`);
console.log(`TimeCreated: ${new Date(metadata.timeCreated)}`);
console.log(`Last Metadata Update: ${new Date(metadata.updated)}`);
console.log(`TurboReplication: ${metadata.rpo}`);
console.log(
`temporaryHold: ${metadata.temporaryHold ? 'enabled' : 'disabled'}`
);
console.log(
`eventBasedHold: ${metadata.eventBasedHold ? 'enabled' : 'disabled'}`
);
if (metadata.retentionExpirationTime) {
console.log(
`retentionExpirationTime: ${new Date(metadata.retentionExpirationTime)}`
);
}
if (metadata.metadata) {
console.log('\n\n\nUser metadata:');
for (const key in metadata.metadata) {
console.log(`${key}=${metadata.metadata[key]}`);
}
}
}
async function listFiles() {
// Lists files in the bucket
const [files] = await bucket.getFiles();
console.log('Files:');
files.forEach(file => {
console.log(file.name);
getMetadata(file.name);
});
}
app.get('/', (req, res) => {
listFiles().catch(console.error);
const name = process.env.NAME || 'World';
res.send(`Hello new ${name}!`);
});
const port = parseInt(process.env.PORT) || 8080;
app.listen(port, () => {
console.log(`helloworld: listening on port ${port}`);
});
Dockerfile 的意思是:创建一个可以构建 Docker 容器的文件。
FROM node:16.15.1-slim
# アプリケーションディレクトリを作成する
COPY . /app/
WORKDIR /app/
EXPOSE 8080
ENTRYPOINT [ "node", "index.js" ]
package.json 文件
{
"name": "helloworld",
"description": "Simple hello world sample in Node",
"version": "1.0.0",
"private": true,
"type": "module",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"engines": {
"node": ">=12.0.0"
},
"author": "Google LLC",
"license": "Apache-2.0",
"dependencies": {
"express": "^4.17.1"
}
}