在Node.js中对IBM对象存储执行CURD操作,可以通过以下步骤实现:
npm install ibm-cos-sdk ibm-cloud-sdk
const IBMCloud = require('ibm-cloud-sdk');
const COS = require('ibm-cos-sdk');
const config = {
endpoint: 's3.us.cloud-object-storage.appdomain.cloud',
apiKeyId: 'YOUR_API_KEY',
ibmAuthEndpoint: 'https://iam.cloud.ibm.com/identity/token',
serviceInstanceId: 'YOUR_SERVICE_INSTANCE_ID',
bucketName: 'YOUR_BUCKET_NAME'
};
const cos = new COS.S3(config);
const createBucket = async () => {
const params = {
Bucket: config.bucketName,
CreateBucketConfiguration: {
LocationConstraint: 'us-standard'
}
};
try {
await cos.createBucket(params).promise();
console.log('Bucket created successfully');
} catch (err) {
console.error('Error creating bucket:', err);
}
};
createBucket();
const uploadObject = async () => {
const params = {
Bucket: config.bucketName,
Key: 'example.txt',
Body: 'Hello, IBM Object Storage!'
};
try {
await cos.putObject(params).promise();
console.log('Object uploaded successfully');
} catch (err) {
console.error('Error uploading object:', err);
}
};
uploadObject();
const downloadObject = async () => {
const params = {
Bucket: config.bucketName,
Key: 'example.txt'
};
try {
const data = await cos.getObject(params).promise();
console.log('Object downloaded successfully:', data.Body.toString());
} catch (err) {
console.error('Error downloading object:', err);
}
};
downloadObject();
const updateObject = async () => {
const params = {
Bucket: config.bucketName,
Key: 'example.txt',
Body: 'Updated content'
};
try {
await cos.putObject(params).promise();
console.log('Object updated successfully');
} catch (err) {
console.error('Error updating object:', err);
}
};
updateObject();
const deleteObject = async () => {
const params = {
Bucket: config.bucketName,
Key: 'example.txt'
};
try {
await cos.deleteObject(params).promise();
console.log('Object deleted successfully');
} catch (err) {
console.error('Error deleting object:', err);
}
};
deleteObject();
请注意,以上代码仅为示例,实际使用时需要根据自己的具体情况进行调整。另外,IBM对象存储还提供了许多其他功能和选项,可以根据需要进行进一步的探索和使用。
关于IBM对象存储的更多信息和腾讯云相关产品,可以参考腾讯云对象存储(COS)的官方文档:腾讯云对象存储(COS)。
领取专属 10元无门槛券
手把手带您无忧上云