【发布时间】:2017-09-25 05:59:17
【问题描述】:
快速浏览
我在this 示例中使用Sketch.js 插件。一世 也想使用我预先签名的网址,但它们不起作用。 过期时间设置的足够长(1 天)所以有问题 与 JS 本身。
我有一个 S3 存储桶,用于存储一些受公共保护的音乐。 使用官方的 AWS 开发工具包,我可以生成如下网址:
https://d225******.cloudfront.net/song.m4a?Expires=1493381986&Signature=***&Key-Pair-Id=***
我在我的网站上使用预签名的网址没有任何问题,但在此脚本中不起作用:
<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>
脚本在没有预签名 URL 的情况下可以正常工作(如您在上面的示例中所见),所以 我可以做些什么来在 AudioAnalyser 函数中使用我的预签名 URL ?
【问题讨论】:
-
您需要在某处...可能在 HTTP 响应标头中找到错误消息。
-
@Michael-sqlbot 从控制台我得到
Uncaught (in promise) DOMException: Failed to load because no supported source was found.从网络选项卡我得到Status Code:206 get from server AmazonS3 miss from Cloudfront -
问题。
206表示您只请求并收到了部分内容,这意味着您显然要提出多个请求......并暗示(但不证明)该请求不是第一个提出的,可能不是最后。您将Expires设置在多远的未来?如果您禁用对签名 URL 的要求,并使其可以通过 CloudFront 简单地下载同一存储桶中的对象而无需签名,这是否有效?不清楚这是否正是您所说的与“正常 URL”一起使用时的意思。 -
codepen 示例不想为我加载。 sketch.min.js 因“(阻塞:混合内容)”而失败。切换到 cdnjs 托管副本使其正常工作。 (cdnjs.cloudflare.com/ajax/libs/sketch.js/1.0/sketch.min.js)
标签: javascript amazon-web-services url amazon-s3 amazon-cloudfront