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

时间:2017-04-27 12:26:38

标签: javascript amazon-web-services url amazon-s3 amazon-cloudfront

快速浏览

  

我在Sketch.js示例中使用了this插件。一世   我也希望使用我预先签名的网址,但他们不会工作。   到期时间设置得足够长(1天),因此出现了问题   与JS本身。

我有一个S3存储桶,我存储了一些受公共保护的音乐。 使用官方AWS SDK,我可以生成以下网址:

<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>

我在我的网站上使用预先签名的网址没有任何问题,但是在这个脚本中没有工作:

AudioAnalyser

在没有预先签名的网址的情况下,该脚本运行正常(正如您在上面的示例中看到的那样),因此如何在X[np.sort(X[:,3].argsort()[:3])] 函数中使用我预先签名的网址? ?

2 个答案:

答案 0 :(得分:0)

我看过html5视频标签发出了多个请求。我假设要获取一些元数据,如播放长度和第一帧视频用作缩略图。您可以尝试使用preload属性来防止这种情况。

具体来说,如果剪辑很小,public int binarySearch(Integer[] targetArray, int targetValue, int lowIndex, int highIndex){ if (lowIndex > highIndex) return -1; int midIndex = lowIndex + (highIndex - lowIndex)/2; if (targetValue == targetArray[midIndex]) return midIndex; if(targetArray[midIndex] > targetValue) return binarySearch(targetArray, targetValue, lowIndex, midIndex - 1); else //if(targetArray[midIndex] < targetValue) return binarySearch(targetArray, targetValue, midIndex + 1, highIndex); //return -1; // replace this with your recursive binary search code } 可能就是您需要的一切。如果浏览器必须提出以下请求,那么我认为你会很难。 Here's some relevant info

我认为可以更可靠地工作的另一种方法是根据需要生成临时凭证。

有关详细信息,请参阅文档: - Requesting temp creds - Accessing resources with temp creds

与用于签署AWS请求的JS程序包相结合,例如binoculars/aws-sigv4copy someone else在浏览器中执行此操作。

答案 1 :(得分:-2)

分享您的浏览器错误消息(如果有)。它可能与S3存储桶数据的交叉原点有关。