Beispiel #1
0
/** Set S3 credentials from environment variables if they are availble and return the s3 client object
 * @function configureS3AndGetClient
 */
function configureS3AndGetClient(){
	if(process.env.hasOwnProperty('PYRO_SERVER_S3_KEY')&&process.env.hasOwnProperty('PYRO_SERVER_S3_SECRET')){
		awsSdk.config.update({
			accessKeyId:process.env.PYRO_SERVER_S3_KEY, 
			secretAccesssKey:process.env.PYRO_SERVER_S3_SECRET
		});
		return s3.createClient({
			s3Options:{
				accessKeyId: process.env.PYRO_SERVER_S3_KEY,
				secretAccessKey: process.env.PYRO_SERVER_S3_SECRET
			}
		});
	} else {
		throw Error('Environment not setup properly. Check S3 keys');
	}
}
Beispiel #2
0
function saveToFileOnS3(argBucketName, argFileKey, argFileContents){
	awsSdk.config.update({ accessKeyId: process.env.PYRO_SERVER_S3_KEY, secretAccessKey: process.env.PYRO_SERVER_S3_SECRET });
	var fileType = argFileKey.split('.')[1];
	var contentType;
	if (fileType=='html') {
		contentType = 'text/html'
	} else if(fileType=='js') {
		contentType = 'application/javascript'
	} else if(fileType=='css') {
		contentType = 'text/css'
	} else if(fileType=='json') {
		contentType = 'application/json'
	} else if(fileType=='jpg'||fileType=='jpeg'||fileType=='jpe') {
		contentType = 'image/jpeg'
	} else if(fileType=='png') {
		contentType = 'image/png'
	} else if(fileType=='gif') {
		contentType = 'image/gif'
	} else if(fileType=='svg') {
		contentType = 'image/svg+xml'
	} else {
		contentType = 'application/octet-stream'
	}
	console.log("FILETYPE ====> ",fileType);
	var news3 = new awsSdk.S3();
	console.log('[saveToFileOnS3] saveFile called', arguments);
	var filePath = argFileKey.replace(':', '.');
  var deferred = Q.defer();
  var saveParams = {Bucket:argBucketName, Key:filePath,  Body: argFileContents, ACL: 'public-read', ContentType: contentType};
  console.log('[saveToFileOnS3] saveParams:', saveParams)
  news3.putObject(saveParams, function(err, data){
    if(!err){
      console.log('[saveToFileOnS3] file saved successfully. Returning:', data);
      deferred.resolve(data);
    } else {
      console.log('[saveToFileOnS3] error saving file:', err);
      deferred.reject(err);
    }
  });
  return deferred.promise;
}