Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports = (event, context, callback) => {
const parsedEvent = JSON.parse(event);
const ossEvent = parsedEvent.events[0];
// Required by OSS sdk: OSS region is prefixed with "oss-", e.g. "oss-cn-shanghai"
const ossRegion = `oss-${ossEvent.region}`;
// Create oss client.
const client = new oss({
region: ossRegion,
bucket: ossEvent.oss.bucket.name,
// Credentials can be retrieved from context
accessKeyId: context.credentials.accessKeyId,
accessKeySecret: context.credentials.accessKeySecret,
stsToken: context.credentials.securityToken
});
const objKey = ossEvent.oss.object.key;
console.log('Getting object: ', objKey);
client.get(objKey).then(function(val) {
const newKey = objKey.replace('source/', 'processed/');
return client.put(newKey, val.content).then(function (val) {
console.log('Put object:', val);
callback(null, val);
}).catch(function (err) {
'Example:',
' deploy:',
' type: aliyun',
' bucket: yourBucketName',
' region: yourOSSregion',
' accessKeyId: yourAccessKeyId',
' accessKeySecret: yourAccessKeySecret',
'',
'For more help, you can check the docs: ' + chalk.underline('http://hexo.io/docs/deployment.html') + ' and ' + chalk.underline('https://help.aliyun.com/document_detail/31867.html?spm=5176.doc31950.2.1.WMtDHS')
];
console.log(help.join('\n'));
log.error('config error');
return;
}
var client = new OSS({
region: args.region,
accessKeyId: args.accessKeyId,
accessKeySecret: args.accessKeySecret,
bucket: args.bucket
});
log.info('Uploading files to Aliyun...');
// get all files sync
traverseFiles(publicDir, function (file) {
uploadFileList.push({
uploadPath: getUploadPath(file, pathFn.basename(publicDir)),
file: file
});
});
// {
// accessKeyId: "keyId",
// accessKeySecret: "secret",
// bucket: "terminus-designer",
// region: "oss-cn-hangzhou"
// }
let { accessKeyId, accessKeySecret, bucket, region, targetProtocol, attachment, targetHost } = options
if (!targetHost) {
targetHost = `${bucket}.${region}.aliyuncs.com`
}
if (!(accessKeyId && accessKeySecret && bucket && region)) {
throw new Error("Missing option in options: [accessKeyId, accessKeySecret, bucket, region]")
}
const store = new oss(options)
return {
put: async (filename, file) => {
await util.fileResolve(file)
const opts = attachment === true ? {
headers: {
"Content-Disposition": `attachment;filename=${encodeURIComponent(file.filename)};filename*=UTF-8''${encodeURIComponent(file.filename)}`
}
} : undefined
return store.put(filename, file.path, opts)
},
get: (result) => {
Object.keys(result).map(filename => {
return result[filename] = `${targetProtocol ? `${targetProtocol}:` : ""}//${targetHost}/${result[filename]}`
})
return result
function kv(key, callback) {
const ossclient = new OSS({
region: 'oss-cn-beijing',
accessKeyId: accessKeyId,
accessKeySecret: accessKeySecret,
bucket: bucket
});
ossclient.get(key).then(function (res) {
const value = res.content; // plain text
if (isValidToken(value.toString())) {
callback(null, value);
} else {
renew((newValue) => { // plain text
// process expires_in value
const v = normalize(newValue); // JSON object
// refresh the OOS
ossclient.put(key, Buffer.from(stringify(v))); // buffer type
var OSS = require('ali-oss')
var localEnv = require('../config')
const request = require('request')
var client = new OSS.Wrapper({
region: localEnv.alioss.region,
accessKeyId: localEnv.alioss.AccessKeyId,
accessKeySecret: localEnv.alioss.AccessKeySecret
})
var aliyun = {
// 上传普通文件
upload: async (file, filename) => {
client.useBucket(localEnv.alioss.bucket)
return await client.put(filename, file)
},
// 上传网络图片
uploadUrl: async (url, filename) => {
var stream = require('stream')
var a = new stream.PassThrough()
var OSS = require('ali-oss')
var Helper = require('./helper')
var localEnv = Helper.readLocalEnv()
const fetchConfVal = (str) => {
let matchs = str.match(/^<(.+)>$/)
return matchs ? process.env[matchs[1]] : str
}
var client = new OSS.Wrapper({
region: localEnv.aliyun.region,
accessKeyId: fetchConfVal(localEnv.aliyun.accessKeyId),
accessKeySecret: fetchConfVal(localEnv.aliyun.accessKeySecret)
})
var aliyun = {
// 上传
upload: (file, removePrefix) => {
return new Promise(resolve => {
let fullname = Helper.uploadFileName(file, removePrefix)
client.useBucket(localEnv.aliyun.bucket)
client.put(fullname, file).then((data) => {
resolve()
})
})
}
function fromOssObject(key, callback) {
const ossclient = new OSS({
region: 'oss-cn-beijing',
accessKeyId: accessKeyId,
accessKeySecret: accessKeySecret,
bucket: bucket
});
ossclient.get(key).then(function (res) {
const buf = res.content;
const b64str = buf.toString('base64');
ossclient.delete(key);
callback(null, b64str);
}).catch(function (err) {
callback(err);
});
}
constructor (config) {
super(config)
this.options = config || {}
this.client = new OSS(this.options)
}
const put = function (objectKey, pathToFile) {
let ossclient = new OSS({
region: 'oss-cn-beijing',
accessKeyId: auth.ali.accessKeyId,
accessKeySecret: auth.ali.accessKeySecret,
bucket: bucket
});
ossclient.useBucket(bucket);
return ossclient.put(objectKey, pathToFile);
};