Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug_report] AliOSS uploader didn't work AliOSS上传器不能正确上传文件 #59

Open
Chen571428 opened this issue Dec 16, 2024 · 0 comments
Assignees

Comments

@Chen571428
Copy link
Contributor

Chen571428 commented Dec 16, 2024

情况

利用上传器工厂创建上传器

    @staticmethod
    def create_uploader(uploader_type: str, **kwargs):
        """
        创建上传器实例
        
        Args:
            uploader_type: 上传器类型 ('alioss' 或 'picgo')
            **kwargs: 上传器所需的参数
        """
        if uploader_type.lower() == 'alioss':
            required_params = ['access_key_id', 'access_key_secret', 'endpoint', 'bucket']
            for param in required_params:
                if param not in kwargs:
                    raise ValueError(f"AliOSS uploader requires {param}")
            
            return Ali_OSS(
                OSS_ACCESS_KEY_ID=kwargs['access_key_id'],
                OSS_ACCESS_KEY_SECRET=kwargs['access_key_secret'],
                Endpoint=kwargs['endpoint'],
                Bucket=kwargs['bucket']
            )
            
        elif uploader_type.lower() == 'picgo':
            endpoint = kwargs.get('endpoint', 'http://127.0.0.1:36677')
            return PicGO(endpoint=endpoint)
            
        else:
            raise ValueError(f"Unsupported uploader type: {uploader_type}")
uploader = None
        if args.steps is None or 3 in args.steps or args.process_each:
            uploader_params = {}
            if args.uploader == 'picgo':
                # 优先使用命令行参数,如果没有则使用配置文件
                picgo_config = config_manager.get_uploader_config('picgo')
                uploader_params['endpoint'] = args.picgo_endpoint or picgo_config.get('endpoint')
                
            elif args.uploader == 'alioss':
                # 优先使用命令行参数,如果没有则使用配置文件
                oss_config = config_manager.get_uploader_config('alioss')
                uploader_params.update({
                    'access_key_id': args.oss_key_id or oss_config.get('access_key_id'),
                    'access_key_secret': args.oss_key_secret or oss_config.get('access_key_secret'),
                    'endpoint': args.oss_endpoint or oss_config.get('endpoint'),
                    'bucket': args.oss_bucket or oss_config.get('bucket')
                })
                
                # 验证所有必需的参数都已提供
                if not all(uploader_params.values()):
                    raise ValueError("Missing required OSS parameters in both command line and config file")
            
            uploader = UploaderFactory.create_uploader(args.uploader, **uploader_params)

参数传入正确,AliOSS服务端配置正常(使用picgo-alioss时正常),但是Status-code 400

初步判断为

"""
        self.auth = oss2.Auth(OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET)
        self.bucket = oss2.Bucket(self.auth, Endpoint, Bucket)

    def upload_file(self, local_file_path, remote_file_path):
        """Upload a file to Aliyun OSS.

        Args:
            local_file_path (str): The path of the local file to upload.
            remote_file_path (str): The path of the remote file to upload to.

        Returns:
            tuple: A tuple containing the URL of the uploaded file and a boolean indicating whether the upload was successful.
        """
        try:
            self.bucket.put_object_from_file(remote_file_path, local_file_path)
            # * Default think the bucket is public read
            return (
                f"https://{self.bucket.bucket_name}.{self.bucket.endpoint.split('://')[1]}/{remote_file_path}",
                True,
            )
        except Exception as e:
            logging.error(f"Error to upload the file: {local_file_path}, {e}")
            return e, False

此处也许不应该调用bucket.put_object_from_file

@Menghuan1918 Menghuan1918 self-assigned this Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants