我怎样才能让我的亚马逊S3编码功能,了解里面的桶子文件夹?(How can I get my Ama

2019-10-17 04:47发布

我写了下面的功能,使他们处理Amazon的S3编码系统,以保护链路编码我S3链接。 麻烦的是,如果该文件是在桶中,它才会起作用。 如果我做一个子文件夹到水桶,坚持在文件中出现,它不工作。 我究竟做错了什么?

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);
}

Answer 1:

我需要改变我解析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);
}

顺便说一句,上面使用了一些功能,这些功能仅适用于WordPress的。 如果你需要直PHP,你将不得不调整这一点。



文章来源: How can I get my Amazon S3 encoding function to understand subfolders inside buckets?
标签: amazon-s3