简体   繁体   English

Sketch JS,如何在音频标签上使用AWS预签名URL?

[英]Sketch JS, how to use AWS pre-signed URL on audio tag?

Quick look 快速浏览

I'm using Sketch.js plugin in this example. 示例中,我使用的是Sketch.js插件。 I would like to use my pre-signed urls as well but they don't work. 我也想使用我的预签名URL,但是它们不起作用。 Expiration time is set long enough (1 day) so there's something wrong with the JS itself. 到期时间设置得足够长(1天),因此JS本身存在问题。

I have an S3 bucket where I store some music public protected. 我有一个S3存储桶,用于存储一些受公众保护的音乐。 Using the official AWS SDK I can generate urls like: 使用官方的AWS开发工具包,我可以生成以下网址:

https://d225******.cloudfront.net/song.m4a?Expires=1493381986&Signature=***&Key-Pair-Id=***

I'm using pre-signed urls over my website without any problem, but in this script won't work: 我在网站上使用预签名的URL没问题,但是在此脚本中不起作用:

<script>
var ALPHA, AudioAnalyser, COLORS, MP3_PATH, NUM_BANDS, NUM_PARTICLES, Particle, SCALE, SIZE, SMOOTHING, SPEED, SPIN;

MP3_PATH = 'my_presigned_url';

AudioAnalyser = (function() {
  AudioAnalyser.AudioContext = self.AudioContext || self.webkitAudioContext;

  AudioAnalyser.enabled = AudioAnalyser.AudioContext != null;

  function AudioAnalyser(audio, numBands, smoothing) {
    var src;
    this.audio = audio != null ? audio : new Audio();
    this.numBands = numBands != null ? numBands : 256;
    this.smoothing = smoothing != null ? smoothing : 0.3;
    if (typeof this.audio === 'string') {
      src = this.audio;
      this.audio = new Audio();
      this.audio.crossOrigin = "anonymous";
      this.audio.controls = true;
      this.audio.src = src;
    }
    this.context = new AudioAnalyser.AudioContext();
    this.jsNode = this.context.createScriptProcessor(2048, 1, 1);
    this.analyser = this.context.createAnalyser();
    this.analyser.smoothingTimeConstant = this.smoothing;
    this.analyser.fftSize = this.numBands * 2;
    this.bands = new Uint8Array(this.analyser.frequencyBinCount);
    this.audio.addEventListener('canplay', (function(_this) {
      return function() {
        _this.source = _this.context.createMediaElementSource(_this.audio);
        _this.source.connect(_this.analyser);
        _this.analyser.connect(_this.jsNode);
        _this.jsNode.connect(_this.context.destination);
        _this.source.connect(_this.context.destination);
        return _this.jsNode.onaudioprocess = function() {
          _this.analyser.getByteFrequencyData(_this.bands);
          if (!_this.audio.paused) {
            return typeof _this.onUpdate === "function" ? _this.onUpdate(_this.bands) : void 0;
          }
        };
      };
    })(this));

  }

  AudioAnalyser.prototype.start = function() {
    return this.audio.play();
  };

  AudioAnalyser.prototype.stop = function() {
    return this.audio.pause();
  };

  return AudioAnalyser;

})();

Sketch.create({
  particles: [],
  setup: function() {
    var analyser, error, i, intro, j, particle, ref, warning, x, y;
    for (i = j = 0, ref = NUM_PARTICLES - 1; j <= ref; i = j += 1) {
      x = random(this.width);
      y = random(this.height * 2);
      particle = new Particle(x, y);
      particle.energy = random(particle.band / 256);
      this.particles.push(particle);
    }
    if (AudioAnalyser.enabled) {
      try {
        analyser = new AudioAnalyser(MP3_PATH, NUM_BANDS, SMOOTHING);
        analyser.onUpdate = (function(_this) {
          return function(bands) {
            var k, len, ref1, results;
            ref1 = _this.particles;
            results = [];
            for (k = 0, len = ref1.length; k < len; k++) {
              particle = ref1[k];
              results.push(particle.energy = bands[particle.band] / 256);
            }
            return results;
          };
        })(this);
        analyser.start();
        document.getElementById('player-container').appendChild(analyser.audio);
        document.getElementsByTagName("audio")[0].setAttribute("id", "dy_wowaudio"); 
        intro = document.getElementById('intro');
        intro.style.display = 'none';
      } catch (_error) {
        error = _error;
      }
    }
  }
});
// generated by coffee-script 1.9.2
</script>

The script works fine (as you can see in the example above) without a pre-signed url, so what can I do to use my pre-signed urls inside AudioAnalyser function ? 该脚本可以很好地工作(如您在上面的示例中看到的),没有预签名的url,那么如何在AudioAnalyser函数中使用我的预签名的url

I've seen html5 video tags make multiple requests. 我已经看到html5视频标签发出了多个请求。 I assume to fetch some meta-data like play length and the first frame of video to use as a thumbnail. 我假设要获取一些元数据,例如播放长度和视频的第一帧用作缩略图。 You might try playing with the preload attribute to prevent this. 您可以尝试使用preload属性来防止这种情况。

Specifically, if the clip is small, preload="auto" might be everything you need. 具体来说,如果剪辑很小,则preload="auto"可能就是您需要的一切。 If the browser has to make follow requests you're gonna have a hard time I think. 我认为,如果浏览器必须发出跟踪请求,您将会遇到困难。 Here's some relevant info 这是一些相关信息

Another way to go about this that I think may work more reliably is to generate temporary credentials as needed. 我认为可能更可靠地解决此问题的另一种方法是根据需要生成临时凭据。

See the docs for more on this: - Requesting temp creds - Accessing resources with temp creds 有关更多信息,请参阅文档:- 请求临时凭证 - 使用临时凭证访问资源

Combined with a JS package for signing AWS requests like binoculars/aws-sigv4 or copy someone else who's doing this in-browser. 与JS软件包结合使用,以签署诸如双筒望远镜/ aws-sigv4之类的AWS请求或复制在浏览器中执行此操作的其他人。

Share your browser error message if any. 共享您的浏览器错误消息(如果有)。 It may by problem related to cross origin for S3 bucket data. 这可能与S3存储桶数据的交叉原点相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将文件上传到AWS中的预签名URL? - How do I upload a file to a pre-signed URL in AWS? 如何使用 S3 预签名 POST url? - How to use an S3 pre-signed POST url? 使用angular + aws-sdk-js +预签名网址将文件上传到S3 - Uploading a file to S3 with angular + aws-sdk-js + pre-signed url 如何使用我的签名证书生成预签名 URL 以在 Amazon S3 上提出异议 - How to use my signed-certificate to generate pre-signed URL to object at Amazon S3 将AWS javascript sdk putObject与预签名URL一起使用 - Using aws javascript sdk putObject with a pre-signed URL 如何使用预签名的 URL 而不是凭证直接从浏览器上传到 AWS S3? - How to upload to AWS S3 directly from browser using a pre-signed URL instead of credentials? 如何在另一个选项卡中打开 S3 预签名 URL? - How to open an S3 pre-signed URL in another tab? 如何使用后端预签名URL使用其SDK将文件上传到Amazon S3? - How can i use a backend pre-signed url to upload files to Amazon S3 using its SDK? Dropzone 客户端通过文件上传到 AWS 预签名 url 调整大小 - Dropzone client side resize with file upload to AWS pre-signed url AWS S3 使用预签名的 URL 更新映像(Axios-PUT 请求) - AWS S3 update image using pre-signed URL (Axios-PUT Request)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM