我怎样才能让我的亚马逊S3编码功能,了解里面的水桶子文件夹? [英] How can I get my Amazon S3 encoding function to understand subfolders inside buckets?

查看:116
本文介绍了我怎样才能让我的亚马逊S3编码功能,了解里面的水桶子文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的函数来连接code我S3的联系,使他们处理Amazon的S3编码系统,以保护环节。麻烦的是,它只能如果该文件是在桶。如果我做一个子文件夹到水桶,坚持在文件中出现,它不工作。我究竟做错了什么?

 功能连接codeS3($ SURL,$ sAccessKey,$ sSecretKey,$ nExpireMinutes = 5){
  $ sFile =基名($ SURL);
  $ sBucket =基名(str_replace函数('/'$ sFile,'',$ SURL)。);
  $ asQuery =阵列(
    AWSAccessKeyId'=> $ sAccessKey,
  );
  $ nExpireSecs = absint($ nExpireMinutes)* 60;
  $ nExpireSecs =时间()+ absint($ nExpireSecs);
  $ asQuery ['过期'] = $ nExpireSecs;
  $ sAmazonText =GET \ñ\ñ\ N {$ nExpireSecs} \ N / {$ sBucket} / {$ sFile};
  $ asQuery ['签名'] = urlen code(base64_en code((hash_hmac(SHA1,utf8_en code($ sAmazonText),$ sSecretKey,TRUE))));
  $ S = add_query_arg($ asQuery,https://s3.amazonaws.com/{$sBucket}/{$sFile});
  返回esc_url($ S);
}
 

解决方案

我只是改变我解析URL的方式需要的。

 功能连接codeS3($ SURL,$ sAccessKey,$ sSecretKey,$ nExpireMinutes = 5){
  / *
  $ sFile =基名($ SURL);
  $ sBucket =基名(str_replace函数('/'$ sFile,'',$ SURL)。);
  * /
  $ SURL = str_replace函数(的https://,,$ SURL);
  $ SURL = str_replace函数('的http://','',$ SURL);
  $ SURL = str_replace函数(urlen code(的https://),,$ SURL);
  $ SURL = str_replace函数(urlen code('的http://'),'',$ SURL);
  $ asParts =爆炸('/',$ SURL);
  array_shift($ asParts);
  $ sBucket = array_shift($ asParts);
  $ sFile =破灭('/',$ asParts);

  $ asQuery =阵列(
    AWSAccessKeyId'=> $ sAccessKey,
  );
  $ nExpireSecs = absint($ nExpireMinutes)* 60;
  $ nExpireSecs =时间()+ absint($ nExpireSecs);
  $ asQuery ['过期'] = $ nExpireSecs;
  $ sAmazonText =GET \ñ\ñ\ N {$ nExpireSecs} \ N / {$ sBucket} / {$ sFile};
  $ asQuery ['签名'] = urlen code(base64_en code((hash_hmac(SHA1,utf8_en code($ sAmazonText),$ sSecretKey,TRUE))));
  $ S = add_query_arg($ asQuery,https://s3.amazonaws.com/{$sBucket}/{$sFile});
  返回esc_url($ S);
}
 

顺便说一句,上面使用了一些功能,这些功能仅在Word preSS可用。如果你需要直PHP中,你将不得不调整这一点。

I wrote the following function to encode my S3 links so that they handle Amazon's S3 encoding system to protect links. The trouble is that it only works if the file is in the bucket. If I make a subfolder to the bucket and stick the file in there, it's not working. What am I doing wrong?

function encodeS3($sURL,$sAccessKey,$sSecretKey,$nExpireMinutes = 5) {
  $sFile = basename($sURL);
  $sBucket = basename(str_replace('/' . $sFile,'',$sURL));
  $asQuery = array(
    'AWSAccessKeyId' => $sAccessKey,
  );
  $nExpireSecs = absint( $nExpireMinutes ) * 60;
  $nExpireSecs = time() + absint( $nExpireSecs );
  $asQuery[ 'Expires' ] = $nExpireSecs;
  $sAmazonText = "GET\n\n\n{$nExpireSecs}\n/{$sBucket}/{$sFile}";
  $asQuery[ 'Signature' ] = urlencode( base64_encode( ( hash_hmac( 'sha1', utf8_encode( $sAmazonText ), $sSecretKey, TRUE ) ) ) );
  $s = add_query_arg( $asQuery, "https://s3.amazonaws.com/{$sBucket}/{$sFile}" );
  return esc_url($s);
}

解决方案

I just needed to change the way I was parsing the URL.

function encodeS3($sURL,$sAccessKey,$sSecretKey,$nExpireMinutes = 5) {
  /*
  $sFile = basename($sURL);
  $sBucket = basename(str_replace('/' . $sFile,'',$sURL));
  */
  $sURL = str_replace('https://','',$sURL);
  $sURL = str_replace('http://','',$sURL);
  $sURL = str_replace(urlencode('https://'),'',$sURL);
  $sURL = str_replace(urlencode('http://'),'',$sURL);
  $asParts = explode('/',$sURL);
  array_shift($asParts);
  $sBucket = array_shift($asParts);
  $sFile = implode('/',$asParts);

  $asQuery = array(
    'AWSAccessKeyId' => $sAccessKey,
  );
  $nExpireSecs = absint( $nExpireMinutes ) * 60;
  $nExpireSecs = time() + absint( $nExpireSecs );
  $asQuery[ 'Expires' ] = $nExpireSecs;
  $sAmazonText = "GET\n\n\n{$nExpireSecs}\n/{$sBucket}/{$sFile}";
  $asQuery[ 'Signature' ] = urlencode( base64_encode( ( hash_hmac( 'sha1', utf8_encode( $sAmazonText ), $sSecretKey, TRUE ) ) ) );
  $s = add_query_arg( $asQuery, "https://s3.amazonaws.com/{$sBucket}/{$sFile}" );
  return esc_url($s);
}

BTW, the above uses some functions that are only available in WordPress. If you need straight PHP, you'll have to adjust this.

这篇关于我怎样才能让我的亚马逊S3编码功能,了解里面的水桶子文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆