Getting/Setting google 云存储中的元数据 - node.js
Getting/Setting metadata in google cloud storage - node.js
我正在尝试从 Google 云存储文件中设置和检索元数据,但我的尝试似乎不起作用。我的代码是 Kubernetes 节点中的 运行/ 我有这个:
// let's make sure that the source file is still there.
let sourceFile = storage.bucket(sourceFileObject.bucket).file( sourceFileObject.name );
const metadata = {
MD_DL_ERROR: 1
};
console.log( `Setting metadata for ${sourceFileObject.name} to: `, metadata );
sourceFile.setMetadata(metadata, function(err, apiResponse) {
console.log( `NOW GETTING METADATA for: ${sourceFileObject.name}` );
sourceFile.getMetadata( function(err, metadata, apiResponse) {
console.log( "got metadata: ", metadata );
} );
});
但是当它运行时,日志中的内容根本没有显示元数据已设置的任何迹象:
Setting metadata for CA-SACMLS/20022759_000.jpg to: { MD_DL_ERROR: 1 }
NOW GETTING METADATA for: CA-SACMLS/20022759_000.jpg
got metadata: { kind: 'storage#object',
id: 'idx-photos-raw-gs.ihouseprd.com/CA-SACMLS/20022759_000.jpg/1588629892121256',
selfLink: 'https://blah blah blah',
mediaLink: 'https://blah blah blah',
name: 'CA-SACMLS/20022759_000.jpg',
bucket: 'idx-photos-raw-gs.ihouseprd.com',
generation: '1588629892121256',
metageneration: '2',
contentType: 'image/jpg',
storageClass: 'MULTI_REGIONAL',
size: '124923',
md5Hash: 'HASHVAL',
crc32c: 'koOVMQ==',
etag: 'CKiFmcObm+kCEAI=',
timeCreated: '2020-05-04T22:04:52.120Z',
updated: '2020-05-04T22:04:52.509Z',
timeStorageClassUpdated: '2020-05-04T22:04:52.120Z' }
我错过了什么?
我认为这是由于文件元数据的修改延迟造成的(第 2 个请求被触发得非常接近和太快),我在您的代码中添加了一个睡眠函数,看起来像预期的那样工作。
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket('fakebucket');
//sleep definition
const sleep = (waitTimeInMs) => new Promise(resolve => setTimeout(resolve, waitTimeInMs));
const sourceFileObject = myBucket.file('demo.zip');
const metadata = {
metadata: {
rrasd: 'custom',
adsdasdasd: 'go here'
}
};
sourceFileObject.setMetadata(metadata, function(err, apiResponse) {
//wait for the propagation
sleep(250).then(() => {
sourceFileObject.getMetadata(function(err, metadata, apiResponse) {
console.log("got metadata: ", metadata);
})
})
});
};
更新
我更改了代码以避免 const metadata
重新定义并且看起来像预期的那样工作,睡眠承诺不共享元数据变量因此我以前的代码有效,感谢@AndyWallace 提供此提示
const nmetadata = {
metadata: {
rr123809123asd: 'custom'
}
};
sourceFileObject.setMetadata(nmetadata, function(err, apiResponse) {
sourceFileObject.getMetadata( function(err, metadata, apiResponse) {
console.log( "got metadata: ", metadata );
})
});
};
我正在尝试从 Google 云存储文件中设置和检索元数据,但我的尝试似乎不起作用。我的代码是 Kubernetes 节点中的 运行/ 我有这个:
// let's make sure that the source file is still there.
let sourceFile = storage.bucket(sourceFileObject.bucket).file( sourceFileObject.name );
const metadata = {
MD_DL_ERROR: 1
};
console.log( `Setting metadata for ${sourceFileObject.name} to: `, metadata );
sourceFile.setMetadata(metadata, function(err, apiResponse) {
console.log( `NOW GETTING METADATA for: ${sourceFileObject.name}` );
sourceFile.getMetadata( function(err, metadata, apiResponse) {
console.log( "got metadata: ", metadata );
} );
});
但是当它运行时,日志中的内容根本没有显示元数据已设置的任何迹象:
Setting metadata for CA-SACMLS/20022759_000.jpg to: { MD_DL_ERROR: 1 }
NOW GETTING METADATA for: CA-SACMLS/20022759_000.jpg
got metadata: { kind: 'storage#object',
id: 'idx-photos-raw-gs.ihouseprd.com/CA-SACMLS/20022759_000.jpg/1588629892121256',
selfLink: 'https://blah blah blah',
mediaLink: 'https://blah blah blah',
name: 'CA-SACMLS/20022759_000.jpg',
bucket: 'idx-photos-raw-gs.ihouseprd.com',
generation: '1588629892121256',
metageneration: '2',
contentType: 'image/jpg',
storageClass: 'MULTI_REGIONAL',
size: '124923',
md5Hash: 'HASHVAL',
crc32c: 'koOVMQ==',
etag: 'CKiFmcObm+kCEAI=',
timeCreated: '2020-05-04T22:04:52.120Z',
updated: '2020-05-04T22:04:52.509Z',
timeStorageClassUpdated: '2020-05-04T22:04:52.120Z' }
我错过了什么?
我认为这是由于文件元数据的修改延迟造成的(第 2 个请求被触发得非常接近和太快),我在您的代码中添加了一个睡眠函数,看起来像预期的那样工作。
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket('fakebucket');
//sleep definition
const sleep = (waitTimeInMs) => new Promise(resolve => setTimeout(resolve, waitTimeInMs));
const sourceFileObject = myBucket.file('demo.zip');
const metadata = {
metadata: {
rrasd: 'custom',
adsdasdasd: 'go here'
}
};
sourceFileObject.setMetadata(metadata, function(err, apiResponse) {
//wait for the propagation
sleep(250).then(() => {
sourceFileObject.getMetadata(function(err, metadata, apiResponse) {
console.log("got metadata: ", metadata);
})
})
});
};
更新
我更改了代码以避免 const metadata
重新定义并且看起来像预期的那样工作,睡眠承诺不共享元数据变量因此我以前的代码有效,感谢@AndyWallace 提供此提示
const nmetadata = {
metadata: {
rr123809123asd: 'custom'
}
};
sourceFileObject.setMetadata(nmetadata, function(err, apiResponse) {
sourceFileObject.getMetadata( function(err, metadata, apiResponse) {
console.log( "got metadata: ", metadata );
})
});
};