Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon s3 从OneSky提取翻译文件并使用Gulp.js将其发布到AWS S3上_Amazon S3_Gulp - Fatal编程技术网

Amazon s3 从OneSky提取翻译文件并使用Gulp.js将其发布到AWS S3上

Amazon s3 从OneSky提取翻译文件并使用Gulp.js将其发布到AWS S3上,amazon-s3,gulp,Amazon S3,Gulp,我正在编写一个Gulp脚本,以便将一些JSON文件推送到我的S3。 (我正在从OneSky下载翻译文件并将其推送到S3) 我正在使用gulprequiretasks包将我的任务拆分为子文件,因此我的任务文件如下所示: const env = require('gulp-env') const glob = require('glob') const fs = require('fs') const onesky = require('onesky-utils') const path = req

我正在编写一个Gulp脚本,以便将一些JSON文件推送到我的S3。 (我正在从OneSky下载翻译文件并将其推送到S3)

我正在使用
gulprequiretasks
包将我的任务拆分为子文件,因此我的任务文件如下所示:

const env = require('gulp-env')
const glob = require('glob')
const fs = require('fs')
const onesky = require('onesky-utils')
const path = require('path')
const AWS = require('aws-sdk/dist/aws-sdk-react-native')

TRANSLATION_LANGUAGES = ['fr-BE']

// ~~~~ Task ~~~~
module.exports = function() {
  env({ file: '.gulp-env.json' })

  for(var locale of TRANSLATION_LANGUAGES) {
    glob('./locales/' + process.env.ONESKY_BASE_LOCALE + '/*.json', {}, function (er, files) {
      files.forEach(function(file) {
        pullOneSkyFile(file, locale).then(content => {
          // Replace the root key with the current locale
          content = content.replace(process.env.ONESKY_BASE_LOCALE, locale)
          const translationFile = writeFile(file, locale, content)
          pushToAmazonS3(translationFile, locale, content)
        }).catch(error => {
          if (error.message === 'Invalid source file') {
            throw Error('The file', file, 'is not present on OneSky')
          } else {
            console.error('ERROR:', error)
          }
        })
      })
    })
  }
}

// ~~~~ Helper Methods ~~~~
function writeFile(file, locale, content) {
  const languageFolder = './locales/' + locale
  if (!fs.existsSync(languageFolder)) {
    console.log('Creating the folder', languageFolder)
    fs.mkdirSync(languageFolder);
  }

  const filePath = languageFolder + '/' + path.basename(file)
  fs.writeFileSync(filePath, content, 'utf-8');
  console.log('File', filePath, 'successfully updated localy for locale', locale, '.')
  return filePath
}

function pushToAmazonS3(filePath, locale, content) {
  console.log('Pushing the file', path.basename(filePath), 'with locale', locale, 'to Amazon S3...')

  const file = {
    // `uri` can also be a file system path (i.e. file://)
    uri: 'file://' + path.resolve(filePath),
    name: path.basename(filePath),
    type: 'application/json'
  }

  console.log('file', file)

  AWS.config.update({
    region: process.env.AMAZON_S3_REGION,
    accessKeyId: process.env.AMAZON_S3_ACCESS_KEY_ID,
    secretAccessKey: process.env.AMAZON_S3_SECRET_ACCESS_KEY
  })

  var s3 = new AWS.S3();
  s3.putObject({Bucket: 'companion-assets', Key: 'test', Body: content}, function (err) {
    if (err) {
      throw err
    } else {
      console.log('Uploading asset', file)
      fileStream.close()
    }
  })
}

function pullOneSkyFile(file, locale) {
  console.log('Pulling the file', path.basename(file), 'with locale', locale, '...')

  return onesky.getFile({
    apiKey: process.env.ONESKY_API_KEY,
    secret: process.env.ONESKY_SECRET_KEY,
    projectId: process.env.ONESKY_PROJECT_ID,
    language: locale,
    fileName: path.basename(file)
  })
}
执行此任务时,我有以下输出:

[15:55:07] Using gulpfile ~/Developments/companion/gulpfile.js
[15:55:07] Starting 'onesky-pull-aws-s3-push'...
locale fr-BE
Pulling the file screens.login.json with locale fr-BE ...
File ./locales/fr-BE/screens.login.json successfully updated localy for locale fr-BE .
Pushing the file screens.login.json with locale fr-BE to Amazon S3...
file { uri: 'file:///Users/zedtux/Developments/companion/locales/fr-BE/screens.login.json',
  name: 'screens.login.json',
  type: 'application/json' }
/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:7090
                    throw err;
                    ^

NetworkingError: XMLHttpRequest is not defined
    at features.constructor.handleRequest (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:31583:16)
    at executeSend (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:6147:31)
    at Request.SEND (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:6161:11)
    at Request.callListeners (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:5704:20)
    at Request.emit (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:5680:12)
    at Request.emit (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:7742:16)
    at Request.transition (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:7081:12)
    at AcceptorStateMachine.runTo (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:7885:14)
    at /Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:7897:12
    at Request.<anonymous> (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:7097:11)
    at Request.<anonymous> (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:7744:14)
    at Request.callListeners (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:5718:20)
    at callNextListener (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:5698:14)
    at /Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:6063:11
    at finish (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:4746:9)
    at /Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:4764:11
    at Credentials.get (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:15525:8)
    at getAsyncCredentials (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:4758:26)
    at Config.getCredentials (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:4778:11)
    at Request.SIGN (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:6035:24)
    at Request.callListeners (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:5704:20)
    at Request.emit (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:5680:12)
[12:15:45] Using gulpfile ~/Developments/companion/gulpfile.js
[12:15:45] Starting 'i18n-deploy'...
[12:15:45] Finished 'i18n-deploy' after 11 ms
Pulling the file screens.login.json with locale fr-BE ...
file /Users/zedtux/Developments/companion/locales/fr-FR/screens.login.json
path.dirname(file) /Users/zedtux/Developments/companion/locales/fr-FR
File /Users/zedtux/Developments/companion/locales/fr-BE/screens.login.json successfully updated localy for locale fr-BE .
[15:55:07]使用gulpfile~/Developments/companion/gulpfile.js
[15:55:07]正在启动“onesky-pull-aws-s3-push”。。。
地点fr BE
正在拉取具有区域设置fr BE的screens.login.json文件。。。
文件./locales/fr BE/screens.login.json成功更新了locale fr BE的localy。
正在将带有区域设置fr的screens.login.json文件推送到Amazon S3。。。
文件{uri:'file:///Users/zedtux/Developments/companion/locales/fr-BE/screens.login.json',
名称:“screens.login.json”,
类型:'application/json'}
/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:7090
犯错误;
^
NetworkingError:未定义XMLHttpRequest
位于features.constructor.handleRequest(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:31583:16)
在executeSend(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:6147:31)
at Request.SEND(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:6161:11)
at Request.callListeners(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:5704:20)
at Request.emit(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:5680:12)
at Request.emit(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:7742:16)
at Request.transition(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:7081:12)
在AcceptorStateMachine.runTo(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:7885:14)
at/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:7897:12
应要求。(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:7097:11)
应要求。(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:7744:14)
at Request.callListeners(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:5718:20)
在callNextListener(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:5698:14)
at/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:6063:11
完成时(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:4746:9)
at/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:4764:11
在Credentials.get(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:15525:8)
在getAsyncCredentials(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:4758:26)
在Config.getCredentials(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:4778:11)
at Request.SIGN(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:6035:24)
at Request.callListeners(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:5704:20)
at Request.emit(/Users/zedtux/Developments/companion/node_modules/aws sdk/dist/aws sdk react native.js:5680:12)
我知道问题在于AWS库是在web浏览器或React原生应用程序中运行的,但在本例中,我只是在编写glup脚本


我该如何解决这个问题呢?

事实上,我完全重写了所有的内容,以郁闷的方式,现在一切正常

我不再使用
gulprequiretasks
,这是我的代码

gulpfile.js

const awspublish = require('gulp-awspublish')
const env = require('gulp-env')
const gulp = require('gulp')
const oneSkyPull = require('./gulp-plugins/onesky-pull')
const rename = require('gulp-rename')

/*
** Pull translations from OneSky to the locale disk, then upload them to an
*  Amazon S3 bucket.
*/
gulp.task('i18n-deploy', function() {
  env({ file: '.gulp-env.json' }) // Load environment variables

  // Prepare the AWS S3 object
  var publisher = awspublish.create({
    accessKeyId: process.env.AMAZON_S3_ACCESS_KEY_ID,
    secretAccessKey: process.env.AMAZON_S3_SECRET_ACCESS_KEY,
    region: process.env.AMAZON_S3_REGION,
    params: {
      Bucket: 'companion-assets'
    }
  }, {
    cacheFileName: 'aws-s3-cache'
  })

  // Walk through all the base language translation files
  // The { base: 'companion' } here is used in order to keep the folder
  // strucutre on S3, otherwise the files were uploaded at the root of
  // the S3 bucket.
  gulp.src('./locales/' + process.env.ONESKY_BASE_LOCALE + '/*.json', { base: 'companion' })
      .pipe(oneSkyPull({
        baseLocale: process.env.ONESKY_BASE_LOCALE,
        locales: ['fr-BE'], // All translation languages
        oneskyApiKey: process.env.ONESKY_API_KEY,
        oneskySecretKey: process.env.ONESKY_SECRET_KEY,
        oneskyProjectId: process.env.ONESKY_PROJECT_ID
      })) // Fetch translation files from OneSky
      .pipe(rename(function(path) {
        // Remove the `../` part from the absolute path avoiding AWS error
        path.dirname = path.dirname.replace('../', '')
      }))
      .pipe(publisher.publish()) // Push to Amazon S3
})
这是我的OneSky插件文件:

const fs = require('fs')
const gutil = require('gulp-util')
const onesky = require('onesky-utils')
const path = require('path')
const through = require('through2')

const PLUGIN_NAME = 'onesky-pull'

// OneSky function to pull the translation file for the given locale
// and given file.
function oneSkyPullFile(file, locale, options) {
  console.log('Pulling the file', path.basename(file), 'with locale', locale, '...')

  return onesky.getFile({
    apiKey: options.oneskyApiKey,
    secret: options.oneskySecretKey,
    projectId: options.oneskyProjectId,
    language: locale,
    fileName: path.basename(file)
  })
}

// Write the translation files on the disk, creating the folders if needed.
// Returns a gutil.File to be pushed in the stream.
function writeFile(file, baseLocale, locale, content) {
  var languageFolder = path.dirname(file)
  languageFolder = languageFolder.replace(baseLocale, locale)
  if (!fs.existsSync(languageFolder)) {
    console.log('Creating the folder', languageFolder)
    fs.mkdirSync(languageFolder);
  }

  const filePath = languageFolder + '/' + path.basename(file)
  fs.writeFileSync(filePath, content, 'utf-8');
  console.log('File', filePath, 'successfully updated localy for locale', locale, '.')
  return new gutil.File({
    path: filePath,
    contents: new Buffer.from(content)
  })
}

// Plugin main functiomn
module.exports = function(options) {
  options = options || {}

  if (!options.baseLocale)
    throw new gutil.PluginError(PLUGIN_NAME, 'please specify the base locale (baseLocale)')

  if (!options.locales)
    throw new gutil.PluginError(PLUGIN_NAME, 'please specify the locales (locales)')

  if (!options.oneskyApiKey)
    throw new gutil.PluginError(PLUGIN_NAME, 'please specify your OneSky API Key (oneskyApiKey)')

  if (!options.oneskySecretKey)
    throw new gutil.PluginError(PLUGIN_NAME, 'please specify your OneSky Secret Key (oneskySecretKey)')

  if (!options.oneskyProjectId)
    throw new gutil.PluginError(PLUGIN_NAME, 'please specify your OneSky Project ID (oneskyProjectId)')

  return through.obj(function (file, encoding, callback) {
    if (file.isNull()) {
      this.push(file) // Do nothing if no contents
      return callback()
    }

    if (file.isBuffer()) {
      for(var locale of options.locales) {
        oneSkyPullFile(file.path, locale, options).then(content => {
          // OneSky build a JSON file based on the base language file with
          // the target locale translations.
          // So we replace the base locale with the current one.
          content = content.replace(options.baseLocale, locale)
          // Write the file on the disk
          const translationFile = writeFile(file.path, options.baseLocale, locale, content)
          // Add back the vynil file to the stream
          this.push(file)
          // Add the created translation file to the stream (so that it will be
          // available for the next plugins).
          this.push(translationFile)

          return callback()
        }).catch(error => {
          if (error.message === 'Invalid source file') {
            this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'The file', file, 'is not present on OneSky'))
          } else {
            this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'ERROR:' + error))
          }
          return callback()
        })
      }
    }

    if (file.isStream()) {
      this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'ERROR: Streams are not supported'))
      return callback()
    }
  })
}
运行该任务会产生以下输出:

[15:55:07] Using gulpfile ~/Developments/companion/gulpfile.js
[15:55:07] Starting 'onesky-pull-aws-s3-push'...
locale fr-BE
Pulling the file screens.login.json with locale fr-BE ...
File ./locales/fr-BE/screens.login.json successfully updated localy for locale fr-BE .
Pushing the file screens.login.json with locale fr-BE to Amazon S3...
file { uri: 'file:///Users/zedtux/Developments/companion/locales/fr-BE/screens.login.json',
  name: 'screens.login.json',
  type: 'application/json' }
/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:7090
                    throw err;
                    ^

NetworkingError: XMLHttpRequest is not defined
    at features.constructor.handleRequest (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:31583:16)
    at executeSend (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:6147:31)
    at Request.SEND (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:6161:11)
    at Request.callListeners (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:5704:20)
    at Request.emit (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:5680:12)
    at Request.emit (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:7742:16)
    at Request.transition (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:7081:12)
    at AcceptorStateMachine.runTo (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:7885:14)
    at /Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:7897:12
    at Request.<anonymous> (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:7097:11)
    at Request.<anonymous> (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:7744:14)
    at Request.callListeners (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:5718:20)
    at callNextListener (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:5698:14)
    at /Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:6063:11
    at finish (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:4746:9)
    at /Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:4764:11
    at Credentials.get (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:15525:8)
    at getAsyncCredentials (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:4758:26)
    at Config.getCredentials (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:4778:11)
    at Request.SIGN (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:6035:24)
    at Request.callListeners (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:5704:20)
    at Request.emit (/Users/zedtux/Developments/companion/node_modules/aws-sdk/dist/aws-sdk-react-native.js:5680:12)
[12:15:45] Using gulpfile ~/Developments/companion/gulpfile.js
[12:15:45] Starting 'i18n-deploy'...
[12:15:45] Finished 'i18n-deploy' after 11 ms
Pulling the file screens.login.json with locale fr-BE ...
file /Users/zedtux/Developments/companion/locales/fr-FR/screens.login.json
path.dirname(file) /Users/zedtux/Developments/companion/locales/fr-FR
File /Users/zedtux/Developments/companion/locales/fr-BE/screens.login.json successfully updated localy for locale fr-BE .